Difference Between Daemon Process and Daemon Thread

daemon

Note: daemon=True must be written before start() 

import time
from multiprocessing import Process
def func():
    for i in range(20):
        time.sleep(0.5)
        print('in func')

def func2():
    print('start : func2')
    time.sleep(5)
    print('end : func2')

if __name__ == '__main__':
    p = Process(target=func)
    p.daemon = True # means to set p as a daemon
    p.start()
    p2 =Process(target=func2)
    p2.start()
    print('in main')
    time.sleep(3)
    print('finished')
    p2.join()

 

 

Daemon thread

import time
from threading import Thread

def daemon_func():
    while True:
        time.sleep(0.5)
        print('daemon thread')

def son_func():
    print('start son')
    time.sleep(5)
    print('end son')

t = Thread(target=daemon_func)
t.daemon = True #Guardian
t.start()
Thread(target=son_func).start()
time.sleep(3)
print('Main thread ends')

 

 

Difference Between Daemon Process and Daemon Thread   

Whether it is a process or a thread, it follows: The guardian xxx will wait for the main xxx to be destroyed after running
Daemon process: Only the code that guards the main process ends. 
Daemon thread: It will guard the end of all other non-daemon threads. The operation is

completed and the non-termination operation
is completed. For the main process, the completion of the operation refers to the completion of the main process code running
to the main thread. It is said that the completion of the operation refers to the completion of all non-daemon threads in the process where the main thread is located, and the main thread is considered to have completed its operation.

 

Daemon:

The main process and child processes do not interfere with each other 
. After the main process is executed, the program will not end, and it will wait for all child processes to end before ending the
daemon process:
it is a child process, and the guardian is the main process
termination condition: the code of the main process ends, the daemon The process also ends


The code of the main process ends, the daemon process ends
The main process needs to reclaim the resources of the daemon process (child process) The
main process waits for all other child processes to end
The main process reclaims the resources of all child processes

Daemon thread:

1. The main thread will wait for the end of the child thread to end 
2. The daemon thread will end with the end of the main thread The
daemon thread will guard the main thread and all child threads

 

 

Daemon thread problem:

1. Does the main thread need to recycle the resources of the sub-thread? 
No, the thread resources belong to the process, so when the process ends, the resources of the thread are naturally recycled.
2. Why does the main thread wait for the sub-thread to end before ending
the main thread ? As the process progresses, the process ends, and all child threads will end. If the
child thread can be successfully executed, the main thread can only wait
3. How does the daemon thread end? When the
main thread ends, the main process also ends, and the daemon thread is The end of the main process ends the


daemon problem
Why does the main process wait for the child process to end before ending? 
Because the main process is responsible for reclaiming some system resources for the child process

 

Reprinted in: https://www.cnblogs.com/whnbky/p/11552894.html

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326560404&siteId=291194637