When the primary process opens a child process, the master process executes in parallel with the child process. When the main process does not immediately end the process, but wait until the end of the child process to end, so as to clean up the zombie process (to the child process corpse).
The first way:
From multiprocessing import Process
Import time
def task (name):
Print ('%s is running '% name)
Time.sleep (3)
Print ('%s is do '% name)
if __name__ = = ' __main__ ':
p = Process (Target=task, kwargs={' name ': ' subprocess 1 '})
# p = Process (Target=task, args= (' sub-process 1 ',))
P.start () # simply sends a signal to the operating system, and then continues to execute without waiting for the child process to complete. However, you can end the main process when the child process is finished.
Print (' master ')
#会执行子进程执行结束后 to end their process.
# Output Results:
# Master
# Child Process 1 is running
# child Process 1 is done
The second way: the way of inheritance
From multiprocessing import Process
Import time
Class Myprocess (process): # Inherit the Process class
def __init__ (self, name):
Super (). __INIT__ ()
Self.name = Name
def run (self): # must override the Run method
Print (' subprocess starting%s '% self.name)
Time.sleep (3)
Print (' End subprocess%s '% self.name)
if __name__ = = ' __main__ ':
p = myprocess (' xxx ')
P.start () # Start automatically binds to the Run method
Print (' main thread ')
# Output Results:
# main Thread
# subprocess Starting XXX
# End subprocess XXX
5.1.2 Network Programming Advanced---Two ways to turn on sub-processes