This example describes the Python creation process fork usage. Share to everyone for your reference. The specific analysis is as follows:
?
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28-29 |
#!coding=utf-8 import OS, traceback import time ' fork () system call is a system call that creates a subprocess on its own process under UNIX, one call, two returns, or a subprocess if the return is 0, if the return value > 0, is the parent process (the return value is the PID of the subprocess) ' ' Source = ten i = 0 try:print ' *********************** ' pid = os.fork () #这里会返回两次, so the following ellipsis will output 2 times the PRI NT ' ... ' if pid = = 0: #子进程 print ' This are child process ' Source = source-1 print ' child process source ', source Tim E.sleep (a) print ' child sleep do ' else: #父进程 print ' This is parent process ' print ' parent process source ', Source Ti Me.sleep (a) print ' Parent sleep done ' print source Except:traceback.print_exc () |
The output is as follows:
?
| 1 2 3 4 5 6 7 8 9 10 11 |
... this is child process child process source is 9 ... this is parent process parent Proces s source is 9 parent-sleep-Done 10 |
I hope this article will help you with your Python programming.