Categories: Python
Tags:

Multiprocessing in Python is a package that allows the creation of processes, enabling parallel execution of tasks. Unlike threads, which run in the same memory space, processes run in separate memory spaces. This makes multiprocessing particularly useful for CPU-bound tasks, as it bypasses Python’s Global Interpreter Lock (GIL) and allows better utilization of multiple CPU cores.

Key Features of Multiprocessing:

  • Each process runs independently in its own memory space.
  • You can achieve true parallelism.
  • Provides Process class to create and manage processes.
  • Includes support for communication between processes using pipes and queues.

Example: Using multiprocessing to Compute Factorials in Parallel

Below is an example that demonstrates the use of multiprocessing:

import multiprocessing
import os

def calculate_factorial(number):
    print(f"Process ID: {os.getpid()} calculating factorial for {number}")
    factorial = 1
    for i in range(1, number + 1):
        factorial *= i
    print(f"Factorial of {number} is {factorial}")

if __name__ == "__main__":
    # List of numbers for which to compute factorials
    numbers = [5, 7, 10, 12]
    
    # Create a list to hold processes
    processes = []

    for number in numbers:
        # Create a process for each number
        process = multiprocessing.Process(target=calculate_factorial, args=(number,))
        processes.append(process)
        process.start()  # Start the process

    # Wait for all processes to complete
    for process in processes:
        process.join()

    print("All processes have finished execution.")

Explanation:

  1. Creating Processes:
    • The multiprocessing.Process class is used to create a process.
    • target specifies the function to be executed by the process.
    • args provides arguments to the target function.
  2. Starting Processes:
    • process.start() starts the execution of the process.
  3. Waiting for Processes:
    • process.join() ensures the main program waits until the process completes execution.

Output:

When you run the script, you will see that each factorial computation runs in a separate process. The output might look like this:

Process ID: 12345 calculating factorial for 5
Process ID: 12346 calculating factorial for 7
Process ID: 12347 calculating factorial for 10
Process ID: 12348 calculating factorial for 12
Factorial of 5 is 120
Factorial of 7 is 5040
Factorial of 10 is 3628800
Factorial of 12 is 479001600
All processes have finished execution.

Benefits of Multiprocessing:

  • Better performance for CPU-bound tasks, as each process can fully utilize a CPU core.
  • Avoids the GIL limitation.

Use Cases:

  • Image or video processing.
  • Mathematical computations.
  • Data transformations or aggregations over large datasets.

Learn Python at Colorstech Institute of Data Analytics