Daemon Threads and Daemons

Source: Internet
Author: User
Tags terminates

A daemon process

Master Process Creation Daemon

One: The daemon terminates after the execution of the main process code is completed

Second: The daemon can no longer open the child process, or throw an exception: Assertionerror:daemonic processes is not allowed to has children

Note: Processes are independent of each other, the main process code is running and the daemon terminates

 fromMultiprocessingImportProcessImport TimeImportRandomclassPiao (Process):def __init__(self,name): Self.name=name Super ().__init__()    defRun (self):Print('%s is piaoing'%self.name) Time.sleep (Random.randrange (1,3))        Print('%s is Piao end'%self.name) P=piao ('Egon') P.daemon=true#be sure to set up before P.start (), set p to daemon, prevent p from creating child processes, and the parent process Code execution ends, p terminates the runP.start ()Print('Master')

Import Timedeffoo ():Print(123) Time.sleep (1)    Print("end123")defBar ():Print(456) Time.sleep (3)    Print("end456") P1=process (target=foo) P2=process (target=bar)if __name__=='__main__': P1.daemon=True P1.start () P2.start ()Print("main-------")
examples of confusing peopleTwo daemon threads

 fromThreadingImportThread,currentthread fromMultiprocessingImportProcessImportos,time,threadingdeftalk1 (): Time.sleep (10)    Print('%s is running'%CurrentThread (). GetName ())deftalk2 (): Time.sleep (2)    Print('%s is running'%CurrentThread (). GetName ())if __name__=='__main__': T1=thread (target=talk1) T2=thread (target=talk2) T1.daemon=True T1.start () T2.start ()Print('Main Thread', Os.getpid ())

 fromThreadingImportThreadImport Timedeffoo ():Print(123) Time.sleep (1)    Print("end123")defBar ():Print(456) Time.sleep (3)    Print("end456") T1=thread (target=foo) T2=thread (target=bar) T1.daemon=Truet1.start () T2.start ( )Print("main-------")
examples of confusing people

The difference between the three daemons and the daemon thread

Whether it is a process or a thread, follow: Guardian xxx will wait for the main xxx to be destroyed after the completion of the operation

It should be emphasized that the operation is not terminated

# 1. For the main process, running complete means that the main process code is running # 2. To the main thread said, run complete refers to the main thread in the process of all non-daemon threads run complete, the main thread is run complete

Detailed Explanation:

# 1 The main process is finished after its code is finished (the daemon is recycled at this point), and then the main process will wait until the non-daemon child processes have finished running to reclaim the child process's resources (otherwise it will produce a zombie process), will be ended, # 2 The main thread runs after the other non-daemons have finished running (the daemon thread is recycled at this point). Because the end of the main thread means the end of the process, the resources of the process as a whole are recycled, and the process must ensure that the non-daemon threads are finished before they end. 

Daemon Threads and Daemons

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.