Of course! In Python, getting the child process ID (PID) is a common task, especially when you've just created a new process using the multiprocessing module.

The key is that the parent process can get the PID of the child it created, but the child process gets the PID of its parent.
Here’s a complete guide, from the simplest case to more advanced scenarios.
The Short and Sweet Answer (Using multiprocessing)
The most common and Pythonic way to create a new process is with the multiprocessing module. When you create a Process object and start it, the parent process can access the child's PID through the .pid attribute of the Process object.
import multiprocessing
import os
import time
def child_process_function():
"""The code that runs in the child process."""
child_pid = os.getpid()
print(f"Child Process: My PID is {child_pid}. My parent's PID is {os.getppid()}.")
time.sleep(2) # Simulate some work
print("Child Process: Finished.")
if __name__ == "__main__":
# The if __name__ == "__main__": guard is important for multiprocessing on some OSes (like Windows)
parent_pid = os.getpid()
print(f"Parent Process: My PID is {parent_pid}.")
# 1. Create a Process object
p = multiprocessing.Process(target=child_process_function)
# 2. Start the process. This is when the OS creates the new process.
p.start()
# 3. NOW the .pid attribute is available on the parent side
child_pid = p.pid
print(f"Parent Process: My child's PID is {child_pid}.")
# 4. Wait for the child process to complete
p.join()
print("Parent Process: Child has finished.")
Running this code will produce output similar to:

Parent Process: My PID is 12345.
Parent Process: My child's PID is 12346.
Child Process: My PID is 12346. My parent's PID is 12345.
Child Process: Finished.
Parent Process: Child has finished.
Detailed Breakdown and Key Concepts
The multiprocessing Module (Recommended)
This is the standard, cross-platform way to create processes in Python. It's designed to have a similar API to the threading module.
Key Steps:
- Import
multiprocessing. - Define a function that contains the code you want the child process to run.
- Create a
multiprocessing.Processobject, passing your function to thetargetargument. - Call
.start()on the process object. This tells the operating system to create a new process and run your target function in it. - Access the child's PID from the parent using the
.pidattribute of theProcessobject. This attribute is only available after.start()has been called. - Call
.join()on the process object in the parent. This pauses the parent process until the child process has finished executing.
The os Module (Lower-Level System Calls)
You can also interact with the operating system directly using the os module. This is less common for simple process creation but is good to know.
os.getpid(): Gets the PID of the current process.os.getppid(): Gets the PID of the parent of the current process.
The multiprocessing module is essentially a more user-friendly wrapper around these lower-level os and subprocess functionalities.

The subprocess Module (For Running External Commands)
If your goal is to run a completely separate program or command (e.g., ls -l, python my_other_script.py), the subprocess module is the right tool. It also gives you access to the child's PID.
import subprocess
import os
if __name__ == "__main__":
parent_pid = os.getpid()
print(f"Parent Process: My PID is {parent_pid}.")
# Run the 'sleep' command for 5 seconds
# The 'shell=True' is needed for simple commands like this
# For more complex cases, it's better to use a list of arguments
process = subprocess.Popen(['sleep', '5'])
# The Popen object has a .pid attribute
child_pid = process.pid
print(f"Parent Process: I launched a command with PID {child_pid}.")
# Wait for the command to complete
process.wait()
print("Parent Process: The command has finished.")
Important Scenarios and Considerations
Getting the Parent's PID from the Child
As shown in the first example, the child can find its parent's PID using os.getppid(). This is useful for the child to report back to or check the status of its parent.
A crucial point: If the parent process terminates before the child, the child's os.getppid() might return a different process (the "init" or "systemd" process, typically PID 1) that has "adopted" the orphaned child.
Handling Multiple Children
If you have a list of child processes, you can store them in a list and iterate to get all their PIDs.
import multiprocessing
import os
import time
def worker():
time.sleep(1)
print(f"Worker {os.getpid()} done.")
if __name__ == "__main__":
processes = []
for i in range(3):
p = multiprocessing.Process(target=worker)
processes.append(p)
p.start()
print(f"Started worker with PID: {p.pid}")
# Wait for all workers to finish
for p in processes:
p.join()
print("All workers have finished.")
Cross-Platform Note (if __name__ == "__main__":)
When you use multiprocessing on Windows, the Python interpreter needs to protect itself from re-importing the main script in each new child process. Placing your main logic inside an if __name__ == "__main__": block is the standard way to ensure the main script is run only once when executed directly, and not when it's imported by a child process. This is a best practice for any script using multiprocessing.
Summary: Which Method to Use?
| Your Goal | Recommended Module | How to Get Child PID |
|---|---|---|
| Run Python code in parallel | multiprocessing |
process_object.pid (after .start()) |
Run an external command/script (e.g., ls, git) |
subprocess |
process_object.pid (on the Popen object) |
| Get the current process's ID | os |
os.getpid() |
| Get the parent of the current process's ID | os |
os.getppid() |
For 99% of cases where you need to create a new process to run Python code, multiprocessing is the correct and most straightforward choice.
