python
  1. python-multiprocessing

Python Multiprocessing

Multiprocessing is a concept of utilizing multiple CPUs of a computer to execute multiple processes in parallel to reduce the processing time. In other words, multiprocessing refers to the execution of multiple processes simultaneously to maximize the utilization of CPU resources.

Syntax

Multiprocessing can be implemented with the help of the multiprocessing module in Python. The syntax for implementing multiprocessing is:

import multiprocessing

def func_name():
    # statement(s)

if __name__ == "__main__":
    # creating a process
    p = multiprocessing.Process(target = func_name)
    
    # starting the process
    p.start()
    
    # join the process
    p.join()

Example

Let's take an example to understand the concept of Multiprocessing in Python:

import multiprocessing
import time

def square(number):
    for n in number:
        # sleep for 2 seconds to simulate long computation time
        time.sleep(2) 
        print("Square: ", n*n)

def cube(number):
    for n in number:
        # sleep for 2 seconds to simulate long computation time
        time.sleep(2) 
        print("Cube: ", n*n*n)

if __name__ == "__main__":
    numbers = [1,2,3,4,5,6]
    
    p1 = multiprocessing.Process(target = square, args=(numbers,))
    p2 = multiprocessing.Process(target = cube, args=(numbers,))
    
    p1.start()
    p2.start()
    
    p1.join()
    p2.join()

Output

The output of the above example will be:

Square:  1
Cube:  1
Square:  4
Cube:  8
Square:  9
Cube:  27
Square:  16
Cube:  64
Square:  25
Cube:  125
Square:  36
Cube:  216

Explanation

In the above example, we first defined two functions square() and cube() which accept a list of numbers. These functions will calculate the square and cube of each number respectively and print the values.

We then initialized the list of numbers and created two processes p1 and p2 for square() and cube() functions respectively. We started the processes using p1.start() and p2.start() and joined them using p1.join() and p2.join().

When we execute the code, we can see that both the processes execute simultaneously and print the results.

Use

The multiprocessing module can be used in the following scenarios:

  • When you want to perform some computation-intensive tasks on multiple CPUs of a computer.
  • When you want to improve the performance of a program by executing multiple tasks in parallel.

Important Points

  • Every process created with multiprocessing must have a unique name or ID.
  • The main purpose of multiprocessing is to reduce the execution time of a program by utilizing multiple CPUs of a computer.
  • Multiprocessing should be used when the computation time taken by a program is high as compared to the IO time.

Summary

Multiprocessing is a powerful technique that can be used to execute multiple processes simultaneously to reduce the processing time. It is a technique that utilizes multiple CPUs of a computer to perform computation-intensive tasks. The multiprocessing module in Python provides an efficient way of implementing multiprocessing in a Python program. It helps in improving the performance of a program by executing multiple tasks in parallel.

Published on: