Python: Show the individual process IDs involved
Multiprocessing Process IDs
In multiprocessing, processes are spawned by creating a Process object.
Write a Python program to show the individual process IDs (parent process, process ID etc.) involved.
Sample Solution:
Python Code:
# Import the 'Process' class from the 'multiprocessing' module.
from multiprocessing import Process
# Import the 'os' module for interacting with the operating system.
import os
# Define a function 'info' to print process-related information.
def info(title):
print(title)
print('module name:', __name__)
print('parent process:', os.getppid())
print('process id:', os.getpid())
# Define a function 'f' that takes a name as an argument.
def f(name):
# Call the 'info' function to print process-related information.
info('function f')
# Print a greeting using the provided name.
print('hello', name)
# Check if the script is being run as the main program.
if __name__ == '__main__':
# Call the 'info' function to print process-related information for the main process.
info('Main line')
# Create a new 'Process' object, specifying the target function 'f' and its arguments.
p = Process(target=f, args=('bob',))
# Start the new process.
p.start()
# Wait for the child process to complete before continuing.
p.join()
Sample Output:
Main line module name: __main__ parent process: 23967 process id: 27986 function f module name: __main__ parent process: 27986 process id: 27987 hello bob
Explanation:
Here is a breakdown of the above Python code:
- Module Imports:
- The code imports the 'Process' class from the 'multiprocessing' module for working with processes.
- The 'os' module is imported for interacting with the operating system.
- Function definitions:
- The 'info' function prints information about the module name, parent process ID, and process ID.
- The 'f' function prints a greeting using the provided name.
- Main Program:
- The script checks if it is being run as the main program using if name == '__main__':.
- Process creation and execution:
- A new 'Process' object is created, specifying the target function 'f' and its arguments.
- The child process is started using p.start().
- The script waits for the child process to complete before continuing using p.join().
Flowchart:

For more Practice: Solve these Related Problems:
- Write a Python program to create multiple processes using the multiprocessing module and print their process IDs.
- Write a Python program to spawn child processes and display both parent and child process IDs using os.getpid().
- Write a Python program to demonstrate process creation and ID retrieval by launching several processes concurrently.
- Write a Python program to print the process IDs of the main process and its spawned child processes using the multiprocessing library.
Go to:
Previous: Write a Python program to make a request to a web page, and test the status code, also display the html code of the specified web page.
Next: Write a Python program to check if two given numbers are Co Prime or not. Return True if two numbers are Co Prime otherwise return false.
Python Code Editor:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.