Sobes.tech
Junior
248

What is multiprocessing?

Answer from AI

sobes.tech AI

Multiprocessing in Python is a way to parallelize task execution by creating and managing multiple independent operating system processes.

Key features:

  • Each process has its own memory address space.
  • Data exchange between processes requires explicit mechanisms (queues, pipes, shared memory).
  • It allows bypassing the Global Interpreter Lock (GIL) limitation in CPython, fully utilizing multi-core processors.

Advantages:

  • Increased performance on multi-core systems.
  • Process isolation: failure of one process does not affect others.

Disadvantages:

  • Greater resource consumption (memory, OS overhead for process creation and switching).
  • Complexity of data exchange.

Used in the multiprocessing module:

import multiprocessing
import time

def worker(num):
    """Function executed in a separate process"""
    print(f"Process {multiprocessing.current_process().name} started with argument {num}")
    time.sleep(1)
    print(f"Process {multiprocessing.current_process().name} finished")

if __name__ == "__main__":
    processes = []
    for i in range(5):
        # Creating a new process
        p = multiprocessing.Process(target=worker, args=(i,))
        processes.append(p)
        # Starting the process
        p.start()

    for p in processes:
        # Waiting for process to finish
        p.join()

    print("All processes are finished.")

Differences from multithreading:

Attribute Multiprocessing Threading
Memory isolation Yes (separate address space) No (shared memory)
CPU utilization Allows using >1 core Limited by GIL (for CPU-bound)
Overhead High Low
Data exchange Explicit Simple (shared memory)

Used for tasks requiring intensive computations (CPU-bound) or high isolation.