Learn what a daemon thread is and how to set it up in Python with small and real-world examples.
In this tutorial, you’ll understand what daemon threads in Python are and how to set them up, and you should have a basic understanding of threads in this tutorial.
What is a daemon thread in Python? A daemon thread is a thread that dies when the main thread dies, also known as a non-blocking thread. Normally, the main thread should wait for the other threads to finish before exiting the program, but if you set the daemon flag, you can let the thread do its job and forget about it, and when the program exits, it will be killed automatically.
This is useful in many situations, let’s say you’re doing some web scraping task, and you want to create a thread that will alert you in an email whenever a new item is inserted into the particular website you’re scraping.
As another example, you might want to create a thread that monitors the log files in your program and alerts you when a critical error occurs.
How does Python understand daemon threads? At the end of this tutorial, we’ll share with you some tutorials that we’ve found useful for working with daemon threads.
Now that we know what a daemon thread is and what it’s used for, let’s take a Python daemon example, the following code starts a non-daemon thread and starts it, and then we end the main thread:
import threading
import time
def func():
while True:
print(f"[{threading.current_thread().name}] Printing this message every 2 seconds")
time.sleep(2)
# initiate the thread to call the above function
normal_thread = threading.Thread(target=func, name="normal_thread")
# start the thread
normal_thread.start()
# sleep for 4 seconds and end the main thread
time.sleep(4)
# the main thread ends
The function should run forever because we set the condition for the loop. After starting the thread, we do a simple sleep in the main thread and exit the program.func()
True
while
What is a daemon thread in Python? However, if you run it, you’ll see that the program keeps running and won’t allow you to quit (only if you close the window):
[normal_thread] Printing this message every 2 seconds
[normal_thread] Printing this message every 2 seconds
[normal_thread] Printing this message every 2 seconds
[normal_thread] Printing this message every 2 seconds
[normal_thread] Printing this message every 2 seconds
...goes forever
As a result, the non-daemon thread makes the program refuse to exit until it is completed.
How does Python understand daemon threads? Here’s an example that starts a daemon thread this time:
import threading
import time
def func_1():
while True:
print(f"[{threading.current_thread().name}] Printing this message every 2 seconds")
time.sleep(2)
# initiate the thread with daemon set to True
daemon_thread = threading.Thread(target=func_1, name="daemon-thread", daemon=True)
# or
# daemon_thread.daemon = True
# or
# daemon_thread.setDaemon(True)
daemon_thread.start()
# sleep for 10 seconds and end the main thread
time.sleep(4)
# the main thread ends
In the Python daemon thread example above, we now pass to threading. Thread
class to indicate that it is a daemon thread, and you can also access the property or use the method.daemon=TruedaemonTruesetDaemon(True)
Let’s execute the procedure:
[daemon-thread] Printing this message every 2 seconds
[daemon-thread] Printing this message every 2 seconds
This time, once the main thread has done its job, the daemon thread will also be automatically killed and exited from the program.
Real-world examples
Here are some tutorials we have using daemon threads to accomplish tasks:
- Make a keylogger in Python: a daemon thread for sending keyloggers to our email or saving to a file.
- Make a port scanner in Python: Multiple daemon threads to scan ports.
- Brute-force FTP server with Python: Multiple daemon threads attempt an FTP login.
- Make a chat application in Python: a daemon thread that listens for incoming messages from the network and prints them to the console.