After the child process is set to the daemon, the child process terminates after the main process code execution finishes (not the process exits) .
If the child process is not set as a daemon, the master process waits for the child to finish before ending the main process.
1. The daemon settings must be set before start.
2. Daemon can no longer open child processes.
fromMultiprocessingImportProcessImport TimedefWork (N): Time.sleep (n)Print('daemon process ..... ')if __name__=='__main__': P= Process (Target=work, args= (3,)) P.daemon=True P.start ()#P.join () Print('within the main process') #Output Result:#within the main process#Description: The child process has not been executed because the main process has ended.
The validation daemon is no longer able to open child processes:
fromMultiprocessingImportProcessImport TimedefWork (N): Time.sleep (n)Print('daemon process ..... ') PP1= Process (Target=time.sleep, args= (3,)) Pp1.start ()if __name__=='__main__': P= Process (Target=work, args= (3,)) P.daemon=True P.start () p.join ()Print('within the main process')#Output Result:#within the main process#Description: The child process has not been executed because the main process has ended.
Output Result:
Error:
Assertionerror:daemonic processes is not allowed to the children Daemon cannot have child processes
daemon process ..... Process Process-1: Traceback (most recent): File"/library/frameworks/python.framework/versions/3.6/lib/python3.6/multiprocessing/process.py", line 258,inch_bootstrap self.run () File"/library/frameworks/python.framework/versions/3.6/lib/python3.6/multiprocessing/process.py", line 93,inchRun Self._target (*self._args, * *Self._kwargs) File"/data/bealla/work/luffy/projects/zuoye/network/test2.py", Line 9,inchWork Pp1.start () File"/library/frameworks/python.framework/versions/3.6/lib/python3.6/multiprocessing/process.py", Line 103,inchStart'Daemonic Processes is not allowed to has children'Assertionerror:daemonic Processes is notallowed to children within the main process
Daemon Exercises:
The daemon terminates after the main process code has finished executing.
fromMultiprocessingImportProcessImport Timedeffoo ():Print(123) Time.sleep (1) Print("end123")defBar ():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-------")
Output:
Main-------456end456
View Code
5.1.6 Daemon Process Daemon