Last Updated on September 12, 2022
The main thread is the default thread within a Python process.
In this tutorial you will discover the main thread and how to access it.
Let’s get started.
What is the Main Thread?
Each Python process is created with one default thread referred to as the “main thread“.
When you execute a Python program, it is executing in the main thread.
The main thread can be thought of as the default thread within your Python process.
In normal conditions, the main thread is the thread from which the Python interpreter was started.
— threading — Thread-based parallelism
The main thread in each Python process always has the name “MainThread” and is not a daemon (background) thread.
This means that once the main thread exits, the Python process will exit, assuming there are no other non-daemon threads running.
There is a “main thread” object; this corresponds to the initial thread of control in the Python program. It is not a daemon thread.
— threading — Thread-based parallelism
Now that we know what the main thread is, let’s look at how we might access it.
Run loops using all CPUs, download your FREE book to learn how.
How to Access the Main Thread
Each process in Python has a threading.Thread instance associated with it.
It can be helpful to retrieve the threading.Thread instance for the main thread so that it can be queried, such as for debugging or logging purposes. Attributes such as the name and the identifier and the native thread identifier can be retrieved.
There are two main ways to access the main thread.
- threading.current_thread(): Get a threading.Thread instance for the current thread.
- threading.main_thread(): Get the threading.Thread for the main thread.
We can acquire a threading.Thread instance that represents the main thread by calling the threading.current_thread() function from within the main thread.
Recall that the main thread is the default thread you code is running within in any Python process.
For example:
1 2 3 |
... # get the main thread thread = current_thread() |
We can also get a threading.Thread instance for the main thread from any thread by calling the threading.main_thread() function from any thread.
For example:
1 2 3 |
... # get the main thread thread = main_thread() |
Now that we know how to access the main thread, let’s look at some worked examples.
Access the Main Thread via the Current Thread
If we are executing code in the main thread already, we can access the main thread via the threading.current_thread() function.
The example below demonstrates how to get the current thread, which is the main thread, and reports its properties.
1 2 3 4 5 6 7 |
# SuperFastPython.com # example of getting the current thread for the main thread from threading import current_thread # get the main thread thread = current_thread() # report properties for the main thread print(f'name={thread.name}, daemon={thread.daemon}, id={thread.ident}') |
Running the example retrieves a threading.Thread instance for the current thread which is the main thread, then reports the details.
Note, your main thread will have a different identifier each time the program is run.
1 |
name=MainThread, daemon=False, id=4386115072 |
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
Access the Main Thread Directly
If we are executing code in the main thread or in any other thread within the process, we can access the main thread directly via the threading.main_thread() function.
The example below demonstrates how to get the main thread directly and reports the properties of the threading.Thread instance.
1 2 3 4 5 6 7 |
# SuperFastPython.com # example of getting the main thread from threading import main_thread # get the main thread thread = main_thread() # report properties for the main thread print(f'name={thread.name}, daemon={thread.daemon}, id={thread.ident}') |
Running the example acquires the threading.Thread instance that represents the main thread and reports the thread properties.
Note, your main thread will have a different identifier each time the program is run.
1 |
name=MainThread, daemon=False, id=4644216320 |
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 about the main thread and how to access it in Python.
Do you have any questions?
Ask your questions in the comments below and I will do my best to answer.
Photo by Christopher Burns on Unsplash
Do you have any questions?