Last Updated on September 12, 2022
The main process is the parent process that executes your program.
In this tutorial you will discover the main process in Python.
Let’s get started.
What is the Main Process?
The main process in Python is the process started when you run your Python program.
Recall that a process in Python is an instance of the Python interpreter. We can create and run new child processes via the multiprocessing.Process class.
What makes the main process unique is that it is the first process created when you run your program and the main thread of the process executes the entry point of your program.
As such the main process does not have a parent process.
The main process also has a distinct name, specifically “MainProcess“.
Run loops using all CPUs, download your FREE book to learn how.
How Can the Main Process Be Identified
There are a number of ways that the main process can be identified.
They are:
- The main process has a distinct name of “MainProcess“.
- The main process does not have a parent process.
- The main process is an instance of the multiprocessing.process._MainProcess class.
We can learn more about these aspects of the main process in the following sections.
How to Get the Main Process?
We can get a multiprocessing.Process instance for the main process.
There are a number of ways that we can do this, depending on where exactly we are in the code.
For example, if we are running code in the main process, we can get a multiprocessing.Process instance for the main process via the multiprocessing.current_process() function.
For example:
1 2 3 4 5 6 7 |
# SuperFastPython.com # example of getting the main process from the main process from multiprocessing import current_process # get the process instance process = current_process() # report details of the main process print(process) |
Running the example gets the process instance for the main process.
1 |
<_MainProcess name='MainProcess' parent=None started> |
If we are in a child process of the main process then we can get the main process via the multiprocessing.parent_process() function.
For example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
# SuperFastPython.com # example of getting the main process from a child of the main process from multiprocessing import parent_process from multiprocessing import Process # function executed in a child process def task(): # get the parent process instance process = parent_process() # report details of the main process print(process) # entry point if __name__ == '__main__': # create a new process process = Process(target=task) # start the new process process.start() # wait for the new process to terminate process.join() |
Running the example starts a new child process to execute a custom task() function.
The child process gets the parent process instance which is the main process and reports its details.
1 |
<_ParentProcess name='MainProcess' parent=None unknown> |
Free Python Multiprocessing Course
Download your FREE multiprocessing PDF cheat sheet and get BONUS access to my free 7-day crash course on the multiprocessing API.
Discover how to use the Python multiprocessing module including how to create and start child processes and how to use a mutex locks and semaphores.
What is the Name of the Main Process?
The main process is assigned a distinct name when it is created.
The name of the parent process is ‘MainProcess‘.
It is distinct from the default name of child processes, that are named ‘Process-%d‘, where %d is an integer starting from 1 to indicate the child number created by the parent process, e.g. Process-1.
Nevertheless, process names can be changed any time and should not be relied upon to identify processes uniquely.
We can confirm the name of the main process by getting the parent process instance via the multiprocessing.current_process() function and getting the “name” attribute.
For example:
1 2 3 4 5 6 7 |
# SuperFastPython.com # example of reporting the name of the main process from multiprocessing import current_process # get the main process process = current_process() # report the name of the main process print(process.name) |
Running the example gets the multiprocessing.Process instance for the main process, then reports the name of the process.
1 |
MainProcess |
Overwhelmed by the python concurrency APIs?
Find relief, download my FREE Python Concurrency Mind Maps
What Type is the Main Process?
The main process is a different object type from child processes.
The type of the main process is multiprocessing.process._MainProcess.
It is distinct from the type of child processes which is an instance of multiprocessing.Process, or more specifically multiprocessing.context.Process.
We can confirm this with a worked example by getting the process instance for the main process and reporting its type via the type() built-in function.
For example:
1 2 3 4 5 6 7 |
# SuperFastPython.com # example of reporting the type of the main process from multiprocessing import current_process # get the main process process = current_process() # report the type of the main process print(type(process)) |
Running the example gets the main process instance and reports its type, which we can see is multiprocessing.process._MainProcess.
1 |
<class 'multiprocessing.process._mainprocess'=""> |
How to Check if a Process is the Main Process?
There are three ways we can check if a process is the main process.
They are:
- Check the name of the process.
- Check the parent of the process.
- Check the type of the process.
Let’s take a closer look at each in turn.
Check if Main Process Via Process Name
The first approach is to check if the name of the process is equal to ‘MainProcess‘.
For example:
1 2 3 4 5 6 7 8 9 10 11 |
# SuperFastPython.com # example of checking if a process is the main process via its name from multiprocessing import current_process # return True if the process is the main process, false otherwise def is_main_process(): # process is the main process if its name is 'MainProcess' return current_process().name == 'MainProcess' # check if this is the main process print(f'Main Process: {is_main_process()}') |
Running the example checks the name of the current process which matches the name of the main process, confirming that the current process is the main process.
1 |
Main Process: True |
This is not reliable, as a process can change its own name or have its name changed at any time.
Check if Main Process Via Process Parent
The second approach is to check if the process does not have a parent process.
The parent of the main process is always None.
For example:
1 2 3 4 5 6 7 8 9 10 11 |
# SuperFastPython.com # example of checking if a process is the main process via its parent from multiprocessing import parent_process # return True if the process is the main process, false otherwise def is_main_process(): # process is the main process if it has no parent process return parent_process() == None # check if this is the main process print(f'Main Process: {is_main_process()}') |
Running the example checks if the parent process of the current process is None and if so confirms that the current process is the main process.
1 |
Main Process: True |
This might be the most reliable method for identifying the main process.
Check if Main Process Via Process Type
The third approach is to check if the type of the current process is multiprocessing.process._MainProcess.
This is different from all child processes, which are typically an instance of multiprocessing.context.Process.
We cannot compare types to multiprocessing.process._MainProcess directly because it is a private type, which is deleted after it is used.
One workaround is to check if a process is not an instance of multiprocessing.Process, in which case we might assume it is the parent process.
This can be achieved by getting the type of the current process, then checking if it is not a subtype of the Process class via the issubclass() built-in function.
For example:
1 2 3 4 5 6 7 8 9 10 11 12 |
# SuperFastPython.com # example of checking if a process is the main process via its type from multiprocessing import current_process from multiprocessing import Process # return True if the process is the main process, false otherwise def is_main_process(): # process is the main process if it has a specific type return not issubclass(type(current_process()), Process) # check if this is the main process print(f'Main Process: {is_main_process()}') |
Running the example checks that the type of the current process is not a subtype of the multiprocessing.Process instance, which in the case of the main process this is the case.
1 |
Main Process: True |
This may not be the most reliable way of checking for the main process via type information. If you have an improvement, let me know in the comments.
Further Reading
This section provides additional resources that you may find helpful.
Python Multiprocessing Books
- Python Multiprocessing Jump-Start, Jason Brownlee (my book!)
- Multiprocessing API Interview Questions
- Multiprocessing API Cheat Sheet
I would also recommend specific chapters in the books:
- Effective Python, Brett Slatkin, 2019.
- See: Chapter 7: Concurrency and Parallelism
- High Performance Python, Ian Ozsvald and Micha Gorelick, 2020.
- See: Chapter 9: The multiprocessing Module
- Python in a Nutshell, Alex Martelli, et al., 2017.
- See: Chapter: 14: Threads and Processes
Guides
- Python Multiprocessing: The Complete Guide
- Python Multiprocessing Pool: The Complete Guide
- Python ProcessPoolExecutor: The Complete Guide
APIs
References
Takeaways
You now know about the main process in Python.
Do you have any questions?
Ask your questions in the comments below and I will do my best to answer.
Photo by redcharlie on Unsplash
Charles "Chuck" Wegrzyn says
Just an FYI – every process save one, in Linux has a parent. You seem to be confusing the idea of the “process” that is spinning off other processes using multiprocessing as not having a “parent” but that is technically incorrect.
Jason Brownlee says
Agreed, more broadly.
Here, the scope is limited to the main python process and child processes it creates.