In this tutorial we will be learning about Python Daemon Thread. In our previous tutorial we learned about Python getattr() function.
Python Daemon Thread
To start with this tutorial, you should have basic knowledge about threads. Basically there are two types of thread. One is daemon thread. Another is non-daemon thread.
While a non-daemon thread blocks the main program to exit if they are not dead. A daemon thread runs without blocking the main program from exiting. And when main program exits, associated daemon threads are killed too.
Python daemon thread example
We have a simple program where we are creating two threads. One of them will take longer time to execute because we have added sleep of 2 seconds. Let’s run the below program and observe the output.
import threading
import time
def print_work_a():
print('Starting of thread :', threading.currentThread().name)
time.sleep(2)
print('Finishing of thread :', threading.currentThread().name)
def print_work_b():
print('Starting of thread :', threading.currentThread().name)
print('Finishing of thread :', threading.currentThread().name)
a = threading.Thread(target=print_work_a, name='Thread-a')
b = threading.Thread(target=print_work_b, name='Thread-b')
a.start()
b.start()
You will get output like below.
Starting of thread : Thread-a
Starting of thread : Thread-b
Finishing of thread : Thread-b
Finishing of thread : Thread-a
So both the threads executed and then main thread exits and terminates the program.
Now we will make Thread a as a daemon thread. Then you will see the difference in output. So, let’s edit the previous code as following.
import threading
import time
def print_work_a():
print('Starting of thread :', threading.currentThread().name)
time.sleep(2)
print('Finishing of thread :', threading.currentThread().name)
def print_work_b():
print('Starting of thread :', threading.currentThread().name)
print('Finishing of thread :', threading.currentThread().name)
a = threading.Thread(target=print_work_a, name='Thread-a', daemon=True)
b = threading.Thread(target=print_work_b, name='Thread-b')
a.start()
b.start()
Notice the extra argument daemon=True
while creating Thread a. This is how we specify a thread as daemon thread. Below image shows the output by the program now.
Notice that program exits even though daemon thread was running.
When Daemon Threads are useful
In a big project, some threads are there to do some background task such as sending data, performing periodic garbage collection etc. It can be done by non-daemon thread. But if non-daemon thread is used, the main thread has to keep track of them manually. However, using daemon thread the main thread can completely forget about this task and this task will either complete or killed when main thread exits.
Note that you should use daemon thread only for non essential tasks that you don’t mind if it doesn’t complete or left in between.
Reference: Official Documentation
Hi Pankaj,
I ran this code in my local as well as client environment but got “Finishing of thread : Thread-a” out put as well.Seems to me the deamon parameter didnt work. can you help me understand why might this be happening?
Thanks in advance
how to import pandas
First install it using PIP:
pip install pandas
. Then import it asimport pandas as py
.hi pankaj!
hope you are doing great,
can you suggest some method by which i can Exchange data between one main thread and two another threads.
Jeez, it’s like one of the simplest explanations on the internet. You should make a video out of it. Will be useful.
Thanks!
Hmm, I am always thinking to create videos but it takes a lot of time and if there is a change in the technology, I can easily update the article. Same is not possible for videos.
Great explanation! Thanks a lot!
Easily and clear! Good explanation! Nice! =)
This is a good explanation about daemon thread.
In a thread programming we create a two thread. Which one will come first?
perfect example
I ask you to please provide these kind of simple examples in thread like join(),is_Alive() ……
Thanks, I will check it out.