Last Updated on September 12, 2022
You can query the status of a threading.Thread via attributes such as name, daemon and ident.
In this tutorial you will discover how to query the status of a thread in Python.
Let’s get started.
Need to Query Thread Attributes
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.
An instance of the threading.Thread class provides a handle of a thread of execution. As such, it provides attributes that we can use to query properties and the status of the underlying thread.
How can we query attributes of a threading.Thread instance?
Run loops using all CPUs, download your FREE book to learn how.
How to Query Thread Attributes
The threading.Thread class provides instance attributes that can be used to query the status of the thread.
This includes queries such as the thread name and whether the thread is currently alive.
The following provides a list of threading.Thread attributes that we can query:
- name: Get or set the name of the thread.
- ident: Access the unique thread identifier assigned by the Python interpreter.
- native_id: Access the unique native thread identifier assigned by the operating system.
- daemon: Get or set whether the thread is a daemon thread or not.
- is_alive(): Check if the thread is currently running.
Only the “name” and “daemon” attributes can be both accessed (retrieved) and set (assigned) after the threading.Thread has been instantiated. The “daemon” attribute cannot be changed after the start() function has been called to run the thread.
The “ident” and “native_id” attributes are read only and return positive integer values.
The “is_alive()” function (not an attribute) returns a boolean value as to whether the read is currently executing its run() function, e.g. running, or not.
Now that we are familiar with how to query the status of a thread, let’s look at some examples.
Example of Querying Thread Name
Each thread has a name.
Threads are named automatically in a somewhat unique manner within each process with the form “Thread-%d” where %d is the integer indicating the thread number within the process, e.g. Thread-1 for the first thread created.
We can access the name of a thread via the “name” attribute, for example:
1 2 3 |
... # report the thread name print(thread.name) |
The example below creates an instance of the threading.Thread class and reports the default name of the thread.
1 2 3 4 5 6 7 |
# SuperFastPython.com # example of accessing the thread name from threading import Thread # create the thread thread = Thread() # report the thread name print(thread.name) |
Running the example creates the thread and reports the default name assigned to the thread within the process.
1 |
Thread-1 |
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 Querying Thread Daemon
A thread may be a daemon thread.
Daemon thread is the name given to background threads. By default, threads are non-daemon threads.
A Python program will only exit when all non-daemon threads have finished exiting. For example, the main thread is a non-daemon thread. This means that daemon threads can run in the background and do not have to finish or be explicitly exited for the program to end.
We can determine if a thread is a daemon thread via the “daemon” attribute.
1 2 3 |
... # report the daemon attribute print(thread.daemon) |
The example creates an instance of the threading.Thread class and reports whether the thread is a daemon or not.
1 2 3 4 5 6 7 |
# SuperFastPython.com # example of assessing whether a thread is a daemon from threading import Thread # create the thread thread = Thread() # report the daemon attribute print(thread.daemon) |
Running the example reports that the thread is not a daemon thread, the default for new threads.
1 |
False |
Overwhelmed by the python concurrency APIs?
Find relief, download my FREE Python Concurrency Mind Maps
Example of Querying Thread Identifier
Each thread has a unique identifier (id) within a Python process, assigned by the Python interpreter.
The identifier is a read-only positive integer value and is assigned only after the thread has been started.
We can access the identifier assigned to a threading.Thread instance via the “ident” property.
1 2 3 |
... # report the thread identifier print(thread.ident) |
The example below creates a threading.Thread instance and reports the assigned identifier.
1 2 3 4 5 6 7 8 9 10 11 |
# SuperFastPython.com # example of reporting the thread identifier from threading import Thread # create the thread thread = Thread() # report the thread identifier print(thread.ident) # start the thread thread.start() # report the thread identifier print(thread.ident) |
Running the example creates the thread instance then confirms that it does not have an identifier before it is started. The thread is started and its unique identifier is reported.
Note, your identifier will differ as the thread will have a different identifier each time the code is run.
1 2 |
None 123145502363648 |
Example of Querying Thread Native Identifier
Each thread has a unique identifier assigned by the operating system.
Python threads are real native threads, meaning that each thread we create is actually created and managed (scheduled) by the underlying operating system. As such, the operating system will assign a unique integer to each thread that is created on the system (across processes).
The native thread identifier can be accessed via the “native_id” property and is assigned after the thread has been started.
1 2 3 |
... # report the native thread identifier print(thread.native_id) |
The example below creates an instance of a threading.Thread and reports the assigned native thread id.
1 2 3 4 5 6 7 8 9 10 11 |
# SuperFastPython.com # example of reporting the native thread identifier from threading import Thread # create the thread thread = Thread() # report the native thread identifier print(thread.native_id) # start the thread thread.start() # report the native thread identifier print(thread.native_id) |
Running the example first creates the thread and confirms that it does not have a native thread identifier before it was started.
The thread is then started and the assigned native identifier is reported.
Note, your native thread identifier will differ as the thread will have a different identifier each time the code is run.
1 2 |
None 3061545 |
Example of Querying Thread Alive
A thread instance can be alive or dead.
An alive thread means that the run() method of the threading.Thread instance is currently executing.
This means that before the start() method is called and after the run() method has completed, the thread will not be alive.
We can check if a Thread is alive via the is_alive() method.
1 2 3 |
... # report the thread is alive print(thread.is_alive()) |
The example below creates a threading.Thread instance then checks whether it is alive.
1 2 3 4 5 6 7 |
# SuperFastPython.com # example of assessing whether a thread is alive from threading import Thread # create the thread thread = Thread() # report the thread is alive print(thread.is_alive()) |
Running the example creates a new threading.Thread instance then reports that the thread is not alive.
1 |
False |
We can update the example so that the thread sleeps for a moment and then reports the alive status of the thread while it is running.
This can be achieved by setting the “target” argument to the threading.Thread to a lambda anonymous function that calls the time.sleep() function.
1 2 3 |
... # create the thread thread = Thread(target=lambda:sleep(1)) |
We can then start the thread by calling the start() function and report the alive status of the thread while it is running.
1 2 3 4 5 |
... # start the thread thread.start() # report the thread is alive print(thread.is_alive()) |
Finally, we can wait for the thread to finish and report the status of the thread afterward.
1 2 3 4 5 |
... # wait for the thread to finish thread.join() # report the thread is alive print(thread.is_alive()) |
Tying this together, the complete example of checking the alive status of a thread while it is running is listed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
# SuperFastPython.com # example of assessing whether a running thread is alive from time import sleep from threading import Thread # create the thread thread = Thread(target=lambda:sleep(1)) # report the thread is alive print(thread.is_alive()) # start the thread thread.start() # report the thread is alive print(thread.is_alive()) # wait for the thread to finish thread.join() # report the thread is alive print(thread.is_alive()) |
Running the example creates the thread instance and confirms that it is not alive before the start() function has been called.
The thread is then started and blocked for a second, meanwhile we check the alive status in the main thread which is reported as True, the thread is alive.
The thread finishes and the alive status is reported again, showing that indeed the thread is no longer alive.
1 2 3 |
False True False |
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 query the attributes of a thread instance.
Do you have any questions?
Ask your questions in the comments below and I will do my best to answer.
Photo by Lloyd Dirks on Unsplash
Do you have any questions?