Write the test code for Python's next fork process (to illustrate that the problem may not be entirely appropriate):
def Fork (a): Def now (): import datetime return Datetime.datetime.now (). Strftime ("%s.%f") Import OS import time Print now (), a If os.fork () = = 0: print ' sub-process [%s]:%s '% (now (), Os.getpid ()) and while 1: a-=10 print ' child process a value [% s]:%s '% (now (), a) if a < 1: break print ' Prepare to exit subprocess ' #os. _exit (0) # # # You can exit the subprocess here else: print ' parent process [ %s]:%s '% (now (), Os.getpid ()) and while 1: a-=1 print ' A value of the parent process [%s]:%s '% (now (), a) if a < 0: Bre AK time.sleep (1) print ' Wait for the child process to end ... ' try: result = os.wait () if result: print ' child process: ', Result[0], result[1] else: print ' no data! ' Except: print ' exception ... ' print ' parent process ... ' print ' the Last value: ', a #exit (0) # # You can also exit here, note that this is where the parent and child processes are shared, Quitting here will cause the parent process to exit as well.
TIPS:
Os.fork () will have two return values, which are the return values of the parent and child processes, respectively
In the parent process, the value returned by fork is the PID of the child process;
Child process, this return value is 0
The child process replicates the context of the parent process
The parent-child process does not determine the order of execution
Os.fork (), the child process must use Exit () or Os._exit () to exit the child process environment, it is recommended to use the Os._exit ()
This code for Os.fork () to create sub-processes is not very good, Linux is not a problem, it is not available under Windows, and official documents have similar statements:
Note that some platforms including FreeBSD <= 6.3, Cygwin and OS/2 EMX has known issues when using fork () from a Threa Davailability:unix.