Python Multiprocessing

Multiprocessing – A computer can perform multiprocessing of several tasks at the same time. Single system consist of multiple processor to execute various tasks. For example A dual core processor has two processor.

Multiprocessing allows the ability of a system to support more than one processor at the same time. Applications in a multiprocessing system are broken to smaller set of routines that run independently.

The operating system allocates these threads to the processors improving performance of the system.

The module used in python for Multiprocessing is: multiprocessing

import multiprocessing
We have to create an object of class Process. It accepts the following arguments:
target: the method or function executed by process
argument: the arguments needed to be passed to target function

In program below, we created two processes with different target functions:

P1 = multiprocessing.Process(target = funct1(5))
P2 = multiprocessing.Process(target = funct2(10))

starting a process requires to call start method of Process class.
P1.start()
P2.start()

Once the processes starts, the other current program also keeps on executing.

To stop the execution of current program until a process is completed, A join method is used.
P1.join()
P2.join()

Therefore, As a result, the current program will first wait for the completion of p1 and then p2.
Once, they are done the next statements of current program will be executed.

The join() method, used with threading or multiprocessing, is unrelated to str. join() – it is not actually a concatenation. Rather, it just meant to “wait for this [thread/process] to complete”.
The name join is used because the threading module uses join for its Thread object.
join is meant” to wait for a thread to complete.

import multiprocessing

def funct1(a1):
    Divisibilty = a1/2 
    print("First Program Number is:",a1)
    print("Divide by 2 to test for Even number:", Divisibilty )
    if Divisibilty==0:
        print(Divisibilty,": It is an even number")
    else:
        print("Not an even number")

def funct2(a2):
    value= a2 * 2
    print('Second Program Number is:' ,a2)
    print("Multiply by 2 is:",value )
    
P1 = multiprocessing.Process(target = funct1(5))
P2 = multiprocessing.Process(target = funct2(10))

P1.start()
P2.start()

P1.join()
P2.join()

print("Program Completed")

Output

First Program Number is: 5
Divide by 2 to test for Even number: 2.5
Not an even number
Second Program Number is: 10
Multiply by 2 is: 20
Program Completed

Print Processes id’s to understand multiprocessing in details

The program here displays that 2 functions below has two different processing id’s as compare to main script.

Note : The main block always the first to be executed in a program.

os.getpid() from os module used to get the current process id of the program in execution.

Once the processes are completed, the isalive() function is used to see, that they are no longer active.

 #importing the multiprocessing module 
import multiprocessing 
import os 
  
def function1(): 
    # print the process id 
    print("ID of process function1: {}".format(os.getpid())) 
  
def function2(): 
    # printing process id 
    print("ID of process function2: {}".format(os.getpid())) 
  
if __name__ == "__main__": 
    # printing process id of main program
    print("ID of process main: {}".format(os.getpid())) 
  
    # Generating processes 
    P1 = multiprocessing.Process(target=function1) 
    P2 = multiprocessing.Process(target=function2) 
  
    # start the processes 
    P1.start() 
    P2.start() 
  
    # print the process IDs 
    print("ID of process P1: {}".format(P1.pid)) 
    print("ID of process P2: {}".format(P2.pid)) 
  
    # wait for the processes to be finished 
    P1.join() 
    P2.join() 
  
    # processes finished execution
    print("Both processes finished execution") 
  
    # You can check whether the processes are alive or not
    print("P1 is alive: {}".format(P1.is_alive())) 
    print("P2 is alive: {}".format(P2.is_alive())) 

Output

ID of process main: 20840
ID of process P1: 13284
ID of process P2: 21920
Both processes finished execution
P1 is alive: False
P2 is alive: False

Leave a Comment