Summary Python use Fork to create sub-process considerations

Source: Internet
Author: User
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.
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.