Last Updated on September 12, 2022
You can get the parent process via the multiprocessing.parent_process() function.
In this tutorial you will discover how to access the parent process in Python.
Let’s get started.
Need to Get Parent Process
A process is a running instance of a computer program.
Every Python program is executed in a Process, which is a new instance of the Python interpreter. This process has the name MainProcess and has one thread used to execute the program instructions called the MainThread. Both processes and threads are created and managed by the underlying operating system.
Sometimes we may need to create new child processes in our program in order to execute code concurrently.
Python provides the ability to create and manage new processes via the multiprocessing.Process class.
You can learn more about multiprocessing in the tutorial:
In concurrent programming, we sometimes need to get the parent process.
This may be for many reasons, such as:
- Conditionally execute code depending on the process that is being run.
- Determine if the current process is the main process, e.g. no parent.
How can we get the parent process in Python?
Run loops using all CPUs, download your FREE book to learn how.
How to Get the Parent Process
We can access the parent process via the multiprocessing.parent_process() function.
This function returns a multiprocessing.Process instance for the parent of the current process.
For example:
1 2 3 |
... # get the parent process parent = multiprocessing.parent_process() |
If the current process is the main process, then the multiprocessing.parent_process() function will return None.
Recall that the main process is the first process started when we execute a Python program.
You can learn more about the main process in the tutorial:
We can also get the process identifier (PID) of the parent process via the os.getppid() function.
For example:
1 2 3 |
... # get the parent pid parent_pid = os.getppid() |
Now that we know how to access the parent process, let’s look at some worked examples.
Example of Getting the Parent Process for the Main Process
We can explore how to get the parent process for the main process.
In this example we get the parent process of the main process and report its details directly.
Recall that the main process does not have a parent Python process, therefore we expect the multiprocessing.parent_process() function to return the value None.
The complete example is listed below.
1 2 3 4 5 6 7 |
# SuperFastPython.com # example of getting the parent process of the main process from multiprocessing import parent_process # get the parent process parent = parent_process() # report details of the parent process print(f'Parent: {parent}') |
Running the example gets the parent process of the main process and reports its details.
We can see that the main process does not have a parent Python process, as we expected.
1 |
Parent: None |
Next, let’s look at an example of getting the parent process from within a child Python process.
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.
Example of Getting the Parent Process
We can explore how to get the parent process from within a child process.
In this example, we will start a new process that executes a custom function. The custom function will then get the parent process and report its details. We expect the parent process of the child process to be the main process in this case.
First, we can define a function to execute in a new child process.
The function will get the parent process and then report its details directly.
The task() function below implements this.
1 2 3 4 5 6 |
# function executed in a new process def task(): # get the parent process parent = parent_process() # report details of the parent process print(f'Parent: {parent}') |
Next, in the main process, we will first define a new multiprocessing.Process instance and configure it to execute our task function.
1 2 3 |
... # create a new child process child = Process(target=task) |
If you are new to executing a function in a child process, see the tutorial:
Next, the child process is started and the main process blocks until the child process terminates.
1 2 3 |
... # wait for the child process to terminate child.join() |
Tying this together, the complete example is listed below.
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 parent process of a child process from multiprocessing import parent_process from multiprocessing import Process # function executed in a new process def task(): # get the parent process parent = parent_process() # report details of the parent process print(f'Parent: {parent}') # protect the entry point if __name__ == '__main__': # create a new child process child = Process(target=task) # start the child process child.start() # wait for the child process to terminate child.join() |
Running the example first starts the child process.
The main process then blocks until the child process finishes.
The child process gets the multiprocessing.Process instance for the parent process and reports its details. In this case, we can see that the parent process is the main process, as we expected.
1 |
Parent: <_ParentProcess name='MainProcess' parent=None unknown> |
Overwhelmed by the python concurrency APIs?
Find relief, download my FREE Python Concurrency Mind Maps
Example of Getting the Parent PID
We can explore how to get the process identifier or PID for the parent process from within a child process.
Similar to the previous example, in this example we will create a new child process, then get the parent PID from within the child process and report it.
First, we can define the function to execute in a new child process. The child function will get the parent PID and then report it directly.
The task() function below implements this.
1 2 3 4 5 6 |
# function executed in a new process def task(): # get the parent process identifier ppid = getppid() # report details of the parent process identifier print(f'Parent PID: {ppid}') |
Next, in the main process, we can configure a new multiprocessing.Process instance to execute our custom task() function. We can then start the child process and block until the child process has terminated.
1 2 3 4 5 6 7 |
... # create a new child process child = Process(target=task) # start the child process child.start() # wait for the child process to terminate child.join() |
Tying this together, the complete example is listed below.
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 parent process identifier from os import getppid from multiprocessing import Process # function executed in a new process def task(): # get the parent process identifier ppid = getppid() # report details of the parent process identifier print(f'Parent PID: {ppid}') # protect the entry point if __name__ == '__main__': # create a new child process child = Process(target=task) # start the child process child.start() # wait for the child process to terminate child.join() |
Running the example starts the child process.
The main process then blocks and waits for the child process to finish.
The child process then gets the PID of the parent process, which is the PID of the main process in this case, and reports it directly.
1 |
Parent PID: 10850 |
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 how to get the parent 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 Raoul Croes on Unsplash
Do you have any questions?