Python Multiprocessing

Python 12 min min read Updated: Mar 09, 2026 Advanced
Python Multiprocessing
Advanced Topic 6 of 10

Multiprocessing in Python

Multiprocessing is a technique that allows a Python program to run multiple processes simultaneously. Unlike multithreading, where threads share the same memory space, multiprocessing creates separate processes with their own memory. This enables true parallel execution and better performance for CPU-intensive tasks.

Python provides a built-in multiprocessing module that allows developers to run tasks across multiple CPU cores, making programs faster and more efficient.

What is Multiprocessing?

Multiprocessing is the execution of multiple processes at the same time. Each process runs independently and has its own memory space. This helps avoid limitations such as Python’s Global Interpreter Lock (GIL), which affects multithreading.

Multiprocessing is commonly used in tasks such as data processing, machine learning training, scientific computing, and parallel simulations.

Why Use Multiprocessing?

  • Utilizes multiple CPU cores
  • Improves performance for CPU-heavy tasks
  • Allows true parallel execution
  • Handles large-scale data processing efficiently

The multiprocessing Module

Python provides the multiprocessing module to create and manage processes.

python import multiprocessing def print_numbers(): for i in range(5): print(i) process = multiprocessing.Process(target=print_numbers) process.start() process.join()

This program creates a separate process that runs the function independently.

Running Multiple Processes

You can run multiple processes simultaneously using the multiprocessing module.

python import multiprocessing def task(name): print("Running task:", name) p1 = multiprocessing.Process(target=task, args=("Process 1",)) p2 = multiprocessing.Process(target=task, args=("Process 2",)) p1.start() p2.start() p1.join() p2.join()

Both processes execute tasks independently.

Using Process Pools

The Pool class allows distributing tasks across multiple processes automatically.

python from multiprocessing import Pool def square(n): return n * n numbers = [1, 2, 3, 4] with Pool(4) as p: result = p.map(square, numbers) print(result)

This program distributes tasks across multiple CPU cores.

Sharing Data Between Processes

Since processes have separate memory spaces, special objects are required to share data.

python from multiprocessing import Process, Value def add_one(num): num.value += 1 num = Value('i', 10) p = Process(target=add_one, args=(num,)) p.start() p.join() print(num.value)

The Value object allows sharing data between processes.

Using Queue for Communication

The Queue object allows safe communication between processes.

python from multiprocessing import Process, Queue def worker(q): q.put("Hello from process") q = Queue() p = Process(target=worker, args=(q,)) p.start() p.join() print(q.get())

This enables processes to exchange data.

Real-World Example

Multiprocessing is often used in data analysis tasks such as processing large datasets.

python from multiprocessing import Pool def process_data(x): return x * 2 data = list(range(10)) with Pool(4) as pool: results = pool.map(process_data, data) print(results)

This program processes data using multiple CPU cores.

Multiprocessing vs Multithreading

Feature Multithreading Multiprocessing
Execution Multiple threads in one process Multiple processes
Memory Shared memory Separate memory
Best For I/O-bound tasks CPU-bound tasks
Performance Limited by GIL True parallel execution

Best Practices

  • Use multiprocessing for CPU-intensive tasks.
  • Avoid excessive process creation.
  • Use process pools for efficient management.
  • Use queues for safe inter-process communication.

Conclusion

Multiprocessing allows Python programs to achieve true parallel execution by running multiple processes simultaneously. It is especially useful for CPU-heavy operations that require significant computational power.

By using the multiprocessing module effectively, developers can significantly improve performance in applications such as data processing, scientific computing, and machine learning workloads.

In the next tutorial, we will explore Async Programming in Python and understand how Python handles asynchronous tasks efficiently.

Get Newsletter

Subscibe to our newsletter and we will notify you about the newest updates on Edugators