A daemon
The main purpose is to create a child process process: primary process has a task complicated by the need to perform, and that helped me to open sub-process concurrent execution of tasks
The main process creates a child process, then the process is set to defend his process
About daemon need to emphasize two points:
First: the daemon will terminate after the end of the main process code execution
Second: within daemon can no longer turn a child process, otherwise it throws an exception: AssertionError: daemonic processes are not allowed to have children
If we have two tasks need to be executed concurrently, then open a master process and a child process were to carry out ok, if the task child process after the end of the primary process task, it is no longer necessary, then the child process should before it was set to open daemon. The main process code runs end, the daemon immediately terminated
The child has not had time began to hang out
Set the daemon is not running to the child process
Daemon must be turned in before the child turned
from multiprocessing Import Process Import Time DEF Task (name): Print ( " % S IS running " % name) the time.sleep ( . 3 ) Print ( " % S IS DONE " % name) IF the __name__ == " __main__ " : T = process (target = Task, args = ( ' sub-process 1 ' ,)) # daemon must be turned in before turning the child t.daemon = True t.start () Print (" Master " ) '' ' master ' ''
In order to open a child process is complicated by the task, the task child process in the main process is dead, there is no sense in this task, the child process should Daemon
Daemon exercises
Finished in the main process code, as long as the print main process message, p1 will not be executed or die
from multiprocessing import Process import time def foo(): print(123) time.sleep(1) print("end123") def bar(): print(456) time.sleep(3) print("end456") if __name__ == '__main__': p1=Process(target=foo) p2=Process(target=bar) p1.daemon=True p1.start() p2.start() print("main-------") ''' main------- 456 end456 '''