Python’s multiprocessing
module lets you run multiple processes at the same time. Each process has its own unique ID called a Process ID (PID). Also, every process (except the very first one) has a Parent Process ID (PPID), which tells you which process created it.
Knowing the PID and PPID helps you track and manage processes, especially when your program creates many child processes.
This article will show you how to get and use the parent and child process IDs in Python programs using the multiprocessing
module and standard libraries.
Accessing the Current Process ID
Every running process in your system has a unique Process ID (PID). In Python, you can get the PID of the current process using the os.getpid()
function.
This helps you identify which process is running your code at any time.
import os
print(f"Current Process ID: {os.getpid()}")
This code prints the PID of the process that runs it. The operating system assigns this number uniquely to each process.
Getting the Parent Process ID (PPID)
Every process, except the very first one, has a parent process that created it. The Parent Process ID (PPID) tells you which process started the current process.
In Python, you can get the PPID using os.getppid()
.
import os
print(f"Process ID: {os.getpid()}")
print(f"Parent Process ID: {os.getppid()}")
This shows both the current process’s PID and its parent’s PID. Knowing the PPID helps you understand how processes are connected in a hierarchy.
What is the Parent Process here?
When you run this script, the parent process is the one that started your Python script. Usually, this is the shell or terminal (like Bash, PowerShell, or Command Prompt) where you ran the command. So:
os.getpid()
shows the ID of the Python process running your script.os.getppid()
shows the ID of the shell or program that launched your script.
If you create child processes within your Python program (using multiprocessing
), those child processes will have your script’s process as their parent. This helps track the full process tree.
Using multiprocessing to Get Child Process IDs
When you create new processes in Python with the multiprocessing
module, the original process is called the parent, and the new one is the child. Each process has its own unique Process ID (PID), and the child process knows the PID of its parent.
You can get the child’s own PID and its parent’s PID from inside the child process using the os
module’s getpid()
and getppid()
functions.
import os
from multiprocessing import Process
def child_task():
# Print the child's PID
print(f"Child process PID: {os.getpid()}")
# Print the child's parent PID
print(f"Child's parent PID: {os.getppid()}")
if __name__ == "__main__":
# Print the parent process PID
print(f"Parent process PID: {os.getpid()}")
# Create and start the child process
p = Process(target=child_task)
p.start()
p.join()
In this example, the parent process first prints its own PID before creating a child process. When the child starts, it prints its own PID followed by the PID of its parent, which is the original process that spawned it. This demonstrates how a child process can identify both itself and the process that created it. Using os.getppid()
inside the child process reliably provides the parent’s PID, reflecting the direct relationship between the two processes. This information can be helpful in cases where a process needs to confirm or log its position within a process hierarchy.
Getting PIDs in Multiple Child Processes
When you create several child processes from the same parent, each child gets its own unique process ID (PID) assigned by the operating system. However, all these child processes share the same parent process ID (PPID), which is the PID of the process that created them.
This example demonstrates clearly how multiple processes relate in a hierarchy using their PIDs, with the OS managing unique IDs for each process.
import os
from multiprocessing import Process
def worker(name):
print(f"{name} PID: {os.getpid()}, Parent PID: {os.getppid()}")
if __name__ == "__main__":
print(f"Parent PID: {os.getpid()}")
names = ["Fred", "George", "Hermione"]
processes = []
for name in names:
p = Process(target=worker, args=(name,))
p.start()
processes.append(p)
for p in processes:
p.join()
In this example, the parent process starts three children named Fred, George, and Hermione. Each child prints its own PID and the PPID, which is the parent’s PID. You can see that while the children have different PIDs, their parent PID is identical, showing the link back to the creator process. The parent waits for all the children to finish by calling join()
on each, ensuring all output completes before the program ends.
Using multiprocessing.current_process()
for Process Info
Besides using os.getpid()
to get the process ID, Python’s multiprocessing
module provides a convenient way to access more detailed process information through current_process()
. This function returns a process object that contains useful attributes like the process name and its PID.
Using current_process()
complements os.getpid()
by giving a more complete picture of each process, including the custom names you assign.
from multiprocessing import Process, current_process
def task():
proc = current_process()
print(f"{proc.name} - PID: {proc.pid}")
if __name__ == "__main__":
parent = current_process()
print('Parent Process ID: ', parent.pid)
p = Process(target=task, name="Samantha")
p.start()
p.join()
In the example, the child process prints its assigned name (which you can set when creating the process) along with its PID. The name is a Python-level identifier and helps distinguish processes beyond just numeric PIDs. This method is especially handy when you want to manage or track processes by name in your program, not just by their operating system IDs.
Real-World Example: Parent and Child Communication with PIDs
In many multiprocessing programs, the parent process needs to know about its children, including their process IDs (PIDs). This can be useful for tracking, logging, or managing child processes. One way to achieve this is by having child processes send their PIDs back to the parent using a Queue
.
import os
from multiprocessing import Process, Queue
def child_task(queue):
pid = os.getpid()
ppid = os.getppid()
queue.put((pid, ppid))
print(f"Child PID: {pid}, Parent PID: {ppid}")
if __name__ == "__main__":
q = Queue()
processes = []
for name in ["Harry", "Ron"]:
p = Process(target=child_task, args=(q,))
p.start()
processes.append(p)
for p in processes:
p.join()
while not q.empty():
child_pid, parent_pid = q.get()
print(f"Received from child: PID={child_pid}, Parent PID={parent_pid}")
Here, the parent process creates child processes that send their PIDs back through the queue. After all children finish, the parent reads from the queue to receive each child’s PID and parent PID. This example demonstrates how processes can share their identification info dynamically, enabling the parent to keep track of its children during runtime.
Conclusion
In this article, we explored how to get process IDs (PID) and parent process IDs (PPID) in Python using the os
and multiprocessing
modules. You learned how to access the current process’s PID and PPID, both in the parent and child processes. We also saw how to handle multiple child processes, how to get process info via multiprocessing.current_process()
, and how to communicate PIDs between processes using a queue.
Getting PIDs is useful for tracking processes, logging their activity, and debugging multiprocessing programs. Understanding these basics helps you work confidently with Python processes and their relationships.