Last Updated on September 12, 2022
You can configure the name of a thread and whether it is a daemon via the threading.Thread class constructor.
In this tutorial you will discover how to configure a new thread in Python.
Let’s get started.
Need to Configure a Thread
A thread is a thread of execution in a computer program.
Every Python program has at least one thread of execution called the main thread. Both processes and threads are created and managed by the underlying operating system.
Sometimes we may need to create additional threads in our program in order to execute code concurrently.
Python provides the ability to create and manage new threads via the threading.Thread class.
It is possible to assign threads a unique name and to make them become daemon (background) threads, although this requires some configuration.
How can we configure new threads?
Run loops using all CPUs, download your FREE book to learn how.
How to Configure a Thread
We can configure a new threading.Thread via the constructor or via setting attributes before the thread has started.
There are two attributes of the thread that we can configure, they are:
- Thread name: the name used to identify the thread.
- Thread daemon: whether the thread is a background thread (daemon) or not.
Setting a unique or meaningful thread name is helpful when debugging and logging to differentiate it from other possible threads of execution.
Threads are not daemon threads (background threads) by default when created in the main thread. New threads take on the daemon value of the thread in which they are created, so because the main thread is not a daemon thread, new threads created in the main thread too will not be daemon threads.
The python process will run for as there are non-daemon threads executing and will stop as soon as all non-daemon threads finish. This means that daemon threads can be used for helpful background tasks that should not stop the program from exiting.
When creating a new instance of a threading.Thread, we can specify the name of the thread via the “name” argument that takes a string and whether the thread is a daemon thread via the “daemon” boolean argument.
For example:
1 2 3 |
... # create a daemon thread with a custom name thread = Thread(name='MyThread', daemon=True) |
The thread name will default to the format “Thread-%d“, where “%d” is an integer for the thread number created by the process, counted from 1 sequentially up for each new thread. This means that the first new thread you might create would have the default name “Thread-1“.
We can also configure the thread name and whether it is a daemon after the threading.Thread instance has been created via attributes, as long as the thread has not started running (e.g. the start() function has not yet been called).
The “name” attribute can be used to set the thread name.
For example:
1 2 3 4 5 |
... # create a thread with default name and daemon thread = Thread() # set the name thread.name = 'MyThread' |
The “daemon” attribute can be used to set whether the thread is a background thread or not.
For example:
1 2 3 4 5 |
... # create a thread with default name and daemon thread = Thread() # configure to be a background thread thread.daemon = True |
Now that we know how to configure new threads in Python, let’s look at some worked examples.
Example of Configuring Thread Name
Threads can be assigned custom names.
The name of a Thread can be set via the “name” argument in the threading.Thread constructor.
For example:
1 2 3 |
... # create a thread with a custom name thread = Thread(name='MyThread') |
The example below demonstrates how to set the name of a thread in the threading.Thread constructor.
1 2 3 4 5 6 7 |
# SuperFastPython.com # example of setting the thread name in the constructor from threading import Thread # create a thread with a custom name thread = Thread(name='MyThread') # report thread name print(thread.name) |
Running the example creates the thread with the custom name then reports the name of the thread.
1 |
MyThread |
The name of the thread can also be set via the “name” property.
For example:
1 2 3 |
... # set the name thread.name = 'MyThread' |
The example below demonstrates this by creating an instance of a thread and then setting the name via the property.
1 2 3 4 5 6 7 8 9 |
# SuperFastPython.com # example of setting the thread name via the property from threading import Thread # create a thread thread = Thread() # set the name thread.name = 'MyThread' # report thread name print(thread.name) |
Running the example creates the thread and sets the name then reports that the new name was assigned correctly.
1 |
MyThread |
Free Python Threading Course
Download your FREE threading PDF cheat sheet and get BONUS access to my free 7-day crash course on the threading API.
Discover how to use the Python threading module including how to create and start new threads and how to use a mutex locks and semaphores
Example of Configuring a Daemon Thread
Threads can be configured to be “daemon” or “daemonic”, that is, they can be configured as background threads.
A process can only exit once all non-daemon threads have exited. This means that daemon threads can run in the background and do not prevent a Python program from exiting when the “main parts of the program” have finished.
A thread can be configured to be a daemon by setting the “daemon” argument to True in the threading.Thread constructor.
For example:
1 2 3 |
... # create a daemon thread thread = Thread(daemon=True) |
The example below shows how to create a new daemon thread.
1 2 3 4 5 6 7 |
# SuperFastPython.com # example of setting a thread to be a daemon via the constructor from threading import Thread # create a daemon thread thread = Thread(daemon=True) # report if the thread is a daemon print(thread.daemon) |
Running the example creates a new thread and configures it to be a daemon thread via the constructor.
True
We can also configure a thread to be a daemon thread after it has been constructed via the “daemon” property.
For example:
1 2 3 |
... # configure the thread to be a daemon thread.daemon = True |
The example below creates a new threading.Thread instance then configures it to be a daemon thread via the property.
1 2 3 4 5 6 7 8 9 |
# SuperFastPython.com # example of setting a thread to be a daemon via the property from threading import Thread # create a thread thread = Thread() # configure the thread to be a daemon thread.daemon = True # report if the thread is a daemon print(thread.daemon) |
Running the example creates a new thread instance then configures it to be a daemon thread, then reports the daemon status.
1 |
True |
Overwhelmed by the python concurrency APIs?
Find relief, download my FREE Python Concurrency Mind Maps
Further Reading
This section provides additional resources that you may find helpful.
Python Threading Books
- Python Threading Jump-Start, Jason Brownlee (my book!)
- Threading API Interview Questions
- Threading Module API Cheat Sheet
I also recommend specific chapters in the following books:
- Python Cookbook, David Beazley and Brian Jones, 2013.
- See: Chapter 12: Concurrency
- Effective Python, Brett Slatkin, 2019.
- See: Chapter 7: Concurrency and Parallelism
- Python in a Nutshell, Alex Martelli, et al., 2017.
- See: Chapter: 14: Threads and Processes
Guides
- Python Threading: The Complete Guide
- Python ThreadPoolExecutor: The Complete Guide
- Python ThreadPool: The Complete Guide
APIs
References
Takeaways
You now know how to configure a new threading.Thread instance.
Do you have any questions?
Ask your questions in the comments below and I will do my best to answer.
Photo by Justin Clark on Unsplash
Do you have any questions?