Does the daemon thread created by the main thread exit after the main thread (process) exits?
Test with the following code:
DEMO1: Process creates normal thread
#!/usr/bin/python3#FileName:daemonThread.py#Author:lxw#date:2016-02-25ImportThreadingImport TimedefShow (num): Time.sleep (3) Print("In Show (): {0}". Format (num)) with open ("/home/lxw/documents/note","W") as F:f.write ("Hello")defmain (): th= Threading. Thread (Target=show, args= (1,)) #Th.setdaemon (True) Th.start ()Print("main () over. Program Exit!")if __name__=='__main__': Main ()Else: Print("Being imported as a module.")
Operation Result:
LxW documents$ python3 daemonthread.py main () over. Program Exit! 1
"The entire Python program exits when no alive Non-daemon threads is left".
The current process will not end until all the non-daemon threads of the current process have finished.
Demo2: process creation Daemon thread
#!/usr/bin/python3#FileName:daemonThread.py#Author:lxw#date:2016-02-25ImportThreadingImport TimedefShow (num): Time.sleep (3) Print("In Show (): {0}". Format (num)) with open ("/home/lxw/documents/note","W") as F:f.write ("Hello")defmain (): th= Threading. Thread (Target=show, args= (1,)) Th.setdaemon (True) Th.start ()Print("main () over. Program Exit!")if __name__=='__main__': Main ()Else: Print("Being imported as a module.")
Operation Result:
ls daemonthread.py NOTE rmlsdaemonthread.py threadpool_pythonLxW Documents $ python3 daemonthread.py main () over. Program Exit! ls daemonthread.py threadpool_python
From the running result we can see that the daemon thread of the current process is not finished, but the current process can still exit. and after the current process is finished, the daemon thread of the process ends and does not continue to run.
That is, the process exits regardless of the daemon thread, only the non-daemon threads.
With the validation of these two pieces of code, we can get the answer to the title of this article: after the process is finished, the daemon thread of the process ends and does not continue to run.
Reference:
1. Java Basic Java Daemon thread and non-daemon thread although this article is about Java, it is also common to Python.
The daemon thread is not available inside the virtual machine, and the user can set the daemon thread by itself: public final void Setdaemon (Boolean on), but there are a few things to note:
1). Thread.setdaemon (True) must be set before Thread.Start () or run out of a illegalthreadstateexception exception. You cannot set a running regular thread as a daemon thread. (Note: This is a significant difference from the daemon, the daemon is created, let the process out of the original session control + let the process out of the original Process group Control + Let the process out of the original control terminal control; So the language mechanism pinned to the virtual machine is fundamentally different from the system-level language)
2). The new thread that is generated in the daemon thread is also daemon. (This is fundamentally different: the Daemon fork () is no longer the daemon, although it copies the process-related information of the parent process, but the parent process of the subprocess is not the init process, the so-called Daemon essentially says, "the parent process hangs, the init adoption, and then File 0,1,2 are/dev/null, current directory to/")
2. Comments on this blog post
Does Daemon thread Exit with Main thread?