Python thread.setDaemon() can make a python thread become a daemon thread. What is daemon thread? How to use it? We will discuss these topics in this tutorial for python beginners.
What is daemon thread?
If a python thread is a daemon thread, which means if its parent thread is end, it is also end.
How to use daemon thread?
We can use thread.setDaemon(True) to make a thread to be a daemon thread. In order to use daemon thread correctly, you must know which thread is its parent thread.
We will use some examples to show you how to use daemon thread.
The parent thread of a daemon thread is main thread
Look at example below, we create a thread in python main thread, which mean the main thread is the parent thread of this thread.
import threading import time def searchFiles(dir): print("start to search files in "+dir) for i in range(100): time.sleep(1) print("get file "+ str(i)+ " in "+ dir) print("search files end in "+ dir) search_thread = threading.Thread(target=searchFiles, args=["C:\\"]) search_thread.start() print("main thread is started!") time.sleep(10) print("main thread is end!")
search_thread is created in main thread, it will take a long time to seach files in a directory. It is not a daemon thread right now.
Run this python example, we can get result like below.
We will find when main thread is end, however, the search_thread is still running. After search_thread is finished, this python application is over.
We set search_thread to be a daemon thread. We should notice its parent thread is main thread.
search_thread = threading.Thread(target=searchFiles, args=["C:\\"]) search_thread.setDaemon(True) search_thread.start() print("main thread is started!") time.sleep(10) print("main thread is end!")
Run this example again, we will get result like this:
We will find search_thread is end when main thread is end.
The parent thread of a daemon thread is not the main thread
Look at example below:
import threading import time def searchFiles(dir, wait = 1): print("start to search files in "+dir) for i in range(10): time.sleep(wait) print("get file "+ str(i)+ " in "+ dir) print("search files end in "+ dir) def createThread(): sub_thread = threading.Thread(target=searchFiles, args=["C:\\", 3]) sub_thread.setDaemon(True) sub_thread.start() searchFiles(dir="F:\\") search_thread = threading.Thread(target=createThread) #search_thread.setDaemon(True) search_thread.start() print("main thread is started!") time.sleep(5) print("main thread is end!")
There are three thread in this example:
main thread: the main thread of python
search_thread: create a thread and run searchFiles(), its parent is main thread
sub_thread: it is a daemon thread and created in search_thread, which means search_thread is its parent thread.
Because sub_thread is a daemon thread and its parent thread is search_thread, if search_thread is end, sub_thread is also end.
Run this example, we will find the result is:
From the result we can find:
1.search_thread is not a daemon thread, so when main thread is end, it is still running.
2.sub_thread is a daemon thread, search_thread is its parent thread. search_thread is end, it is also end.