Daemon Threads in Python

Daemon Threads in Python with Examples

In this article, I am going to discuss Daemon threads in Python with examples. Please read our previous article where we discussed Important Methods of Thread class in Python

What are Daemon threads?

When a program is running there are some process, related to the program, that will be running in the background supporting the program execution. Such threads or processes which are running in the background are called Daemon Threads. The main objective of Daemon Threads is to provide support for Non-Daemon Threads (like main thread)

Example: Garbage Collector

Whenever Main Thread runs with low memory, immediately PVM runs Garbage Collector to destroy useless objects and to provide free memory, so that Main Thread can continue its execution without having any memory problems.

We can check whether the thread is Daemon or not, by using isDaemon() method of Thread class and also by using daemon property.

Program: Checking whether the Thread is Daemon or not in Python (demo14.py)

from threading import *
print(current_thread().isDaemon())
print(current_thread().daemon)

Output:

Checking whether the Thread is Daemon or not in Python

Note: The main thread is always Non-Daemon thread

setDaemon() method in python:

We can change Daemon nature by using the setDaemon() method of Thread class. In this perspective, let’s know some points.

We have to set a thread as a Daemon before starting it. Once a thread starts, we cannot change its Daemon nature. If we try to change, we will get RuntimeError: cannot set daemon status of active thread

Program: setDaemon() method of Thread class in Python (demo15.py)

from threading import *
def display():
   print("Child Thread")
t=Thread(target=display)
t.start()
t.setDaemon(True)

Output: setDaemon() method of Thread class in Python

Some points to note:
  1. When we start the execution of a program, the Main Thread will automatically start. We don’t explicitly start the Main Thread.
  2. Since, by default, the Main Thread is a Non Daemon, and it starts automatically at the start to program execution, we can’t change its Non Daemon nature to Daemon.
  3. Hence, the Main Thread will always be a Non Daemon thread.
  4. The threads, which we create in the program, will inherit their Daemon nature from the parent thread, by default.
  5. If the Parent Thread is Daemon, then child thread is also Daemon by default.
  6. If the Parent Thread is Non-Daemon, then ChildThread is also Non-Daemon by default.
  7. Whenever the last Non-Daemon Thread terminates, automatically all Daemon Threads will be terminated
Program: Main Thread is always Non-Daemon in Python (demo16.py)
from threading import *
import time

def display():
   for i in range(5):
       print("Child Thread")
       time.sleep(2)

t=Thread(target=display)
#t.setDaemon(True)
t.start()
time.sleep(5)
print("End Of Main Thread")

Output:

Main Thread is always Non-Daemon in Python

In the above program, the child thread, will inherit its daemon nature from parent thread i.e Main Thread, by default. Since both the parent and child are non daemon they will be executed completely.

Program: Main Thread is always Non-Daemon Thread in Python (demo17.py)
from threading import *
import time

def display():
   for i in range(5):
       print("Child Thread")
       time.sleep(2)

t=Thread(target=display)
t.setDaemon(True)
t.start()
time.sleep(5)
print("End Of Main Thread")

Output:

Main Thread is always Non-Daemon Thread in Python

In this program, we have changed the daemon status of the child thread to True before starting it.

The main thread, non daemon, gets completed faster than the child thread, a non daemon. After the execution of a non daemon thread, immediately the daemon thread execution is stopped.

In the next article, I am going to discuss Synchronization in Python. Here, in this article, I try to explain Important Daemon threads in Python with Examples. I hope you enjoy this Daemon threads in Python with Examples article. I would like to have your feedback. Please post your feedback, question, or comments about this article.

Leave a Reply

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