5.1.6 Daemon Process Daemon

Source: Internet
Author: User
Tags terminates

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.