Python Daemon Thread

Filed Under: Python

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.

python daemon thread

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

Comments

  1. Ilya says:

    Jeez, it’s like one of the simplest explanations on the internet. You should make a video out of it. Will be useful.

    Thanks!

    1. Pankaj says:

      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.

  2. Barnabas says:

    Great explanation! Thanks a lot!

  3. Italo says:

    Easily and clear! Good explanation! Nice! =)

  4. Vishnu Chauhan says:

    This is a good explanation about daemon thread.

  5. kalai says:

    In a thread programming we create a two thread. Which one will come first?

  6. srinath says:

    perfect example

    I ask you to please provide these kind of simple examples in thread like join(),is_Alive() ……

    1. Pankaj says:

      Thanks, I will check it out.

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages