Python Basics-Daemon, daemon, daemon non-daemon parallel

Source: Internet
Author: User
Tags terminates

Daemon 1, Guardian 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

Let's look at an example

From multiprocessing Import Processimport os,time,randomdef task ():    print ('%s is running '%os.getpid ())    Time.sleep (2)    print ('%s is done '%os.getpid ())    #守护进程内无法再开启子进程, otherwise throws an exception    # p = Process (Target=time.sleep, Args= (3,))    # P.start () if __name__ = = ' __main__ ':    p=process (target=task)    P.daemon = True #1, must be before P.start ()    P.start ()    print (' master ')

The output results are as follows:

Main

The reason is: The main process starts execution to the P sub-process, because the child process needs to open up memory space, because it takes time, so the main process will first output "master", because the main process is completed, then the daemon p is killed, followed by the main process will be exited

If the above code is modified as follows, plus p.join () This line of code

if __name__ = = ' __main__ ':    p=process (target=task)    P.daemon = True #1, must be P.start () before P.start    (    ) p.join ()    print (' master ')

Then the program will output the following:

14732 is running14732 was done main

Join has been analyzed before, is blocking, the child process is completed before executing the main process, so add join
1, execution to join, is blocking, will execute the child process, then executed, in the execution of the main process
2, it can also be understood that the implementation to join, because the main process print ("master") is not finished, so the daemon will not be killed, continue to execute

1, daemon process, non-daemon process coexist

In the above example, the child process has only one daemon, and after the main process is executed, the daemon will be killed.
, we're looking at a child process that has both a daemon and a non-daemon process

From multiprocessing import processfrom threading import Threadimport time,osdef 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-------")

The output is as follows:

Main-------456end456

The reasons are as follows: Because P1,P2 are child processes, need to open up memory space, it takes time, so it will first output the main process "main", because P1 is a daemon, p2 is a non-daemon process, when the main process is finished (note that the main process has not exited, because there is a P2 non-daemon process), P1 Daemon also back, but there is a P2 non-daemon process, so P2 will execute their own code task, when the P2 execution, then the main process is exited, and then the entire program exited .

Guardian Thread Daemon Child 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.

Let's look at an example first.

 fromMultiprocessingImportProcess fromThreadingImportThreadImportOs,time,randomdeftask ():#T=thread (target=time.sleep,args= (3,) )    #T.start ()    Print('%s is running'%os.getpid ()) Time.sleep (2)    Print('%s is done'%os.getpid ())if __name__=='__main__': T=thread (target=Task) T.daemon=True T.start ()Print('Master')

The output is as follows:

13368 is running master

The reasons are:
In the execution to the daemon thread T, because the mainline Cheng Zi thread is a common piece of memory, so there is no different process to create the respective space, so the execution task code of the child process is output first, so output print ('%s is running '%os.getpid ()), because Time.sleep (2 ), so the main thread "main" will be executed, and then the main thread executes, then even after 2 seconds, since the main thread execution completes, then the child daemon thread exits, so print ('%s is Do '%os.getpid ()) will not be executed

Guardian Child thread non-daemon process coexistence

Let's take a look at an example of a daemon that is not a daemon process.

From threading import Threadimport timedef foo ():    print (123)    Time.sleep (1)    print ("end123") def Bar ():    print (456)    Time.sleep (3)    print ("end456") if __name__ = = ' __main__ ':    t1=thread (target=foo)    t2 = Thread (Target=bar)    t1.daemon=true    t2.start ()    t1.start ()    print ("main-------")

The output is as follows:

456123main-------end123end456

The reasons are:
T1 is a daemon thread, t2 non-daemon thread, with the main thread using a piece of memory, so it will output T1,T1 sub-thread of the task code, so do 456,123 because t1,t2 have sleep time, so the main thread code, and then the main thread said, Run complete refers to the main thread is in the process of all non-daemon threads run complete, the main thread is run, so will execute T1,T2 sleep after the task code, and then the program exits.
We will ask why the T1 daemon thread, will also execute the sleep after the code, not to say that the main thread code execution is complete, the daemon was killed? Here to pay attention to the main thread, said, run complete refers to the main thread is in the process of all the non-daemon threads run complete, the main thread is run complete, then T2 not finished

Turn from:
78710532

Python Basics-Daemon, daemon, daemon non-daemon parallel

Related Article

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.