This example describes the Python creation process fork usage. Share to everyone for your reference. The specific analysis is as follows:
#!coding=utf-8import OS, Tracebackimport time ' fork () system call is a system call that creates a child process under UNIX under its own process, 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 child process) ' Source = 10i = 0try: print ' *********************** ' pid = os.fork () #这里会返回两次, So the ellipsis below will output 2 times print ' ... ' if pid = = 0: #子进程 print "This is a child process" Source = source-1 Print ' Child process source is ', source time.sleep (+) print ' Child sleep done ' else: #父进程 print " This is the parent process " print ' parent process source is ', source Time.sleep (Ten) print ' Parent sleep done ' C16/>print sourceexcept: traceback.print_exc ()
The output is as follows:
... this is a child processchild process source was 9......this is the parent processparent process sour Ce is 10child sleep done9parent sleep Done10
Hopefully this article will help you with Python programming.