Summary Notes for using fork to create sub-processes in Python

Source: Internet
Author: User
Tags exit in
Today, we can see that there are fork sub-processes in others' source code to operate data. However, as fork does not exit in time, the number of Python processes in the system increases and the number of sub-processes increases. I wrote the test code for the fork process under Python (to illustrate this problem is not necessarily 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 () while 1: a-= 10 print 'sub-process a value [% s]: % s' % (now (), a) if a <1: break print 'Prepare to exit the subprocess' # OS. _ exit (0) # Here you can exit the sub-process else: print 'parent process [% s]: % s' % (now (), OS. getpid () while 1: a-= 1 print 'a value of the parent process [% s]: % s' % (now (), a) if a < 0: break time. sleep (1) print 'Wait until the child process ends... 'Try: result = OS. wait () if result: print 'sub-process: ', result [0], result [1] else: print' no data! 'Failed T: print' exception... 'print 'parent process... the final value of 'print ':', a # exit (0) # You can also exit here. Note that this is where both the parent process and child process are shared, exit here will cause the parent process to exit together.

TIPS:

OS. fork () has two returned values: the returned values of the parent process and the child process.
In the parent process, fork returns the PID of the child process;
In the sub-process, the returned value is 0.
The sub-process copies the context of the parent process.
The execution sequence of the Parent and Child processes cannot be determined.
After OS. fork (), the sub-process must use exit () or OS. _ exit () to exit the sub-process environment. We recommend that you use OS. _ exit ()
The code for operating system. fork () to create sub-processes is not very suitable. Linux is no problem and cannot be used in Windows. The official documentation also has a similar statement:

Note that some platforms including FreeBSD <= 6.3, Cygwin and OS/2 EMX have known issues when using fork() from a threadAvailability: 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.