Multiprocessing Basics
Multiprocessing uses separate processes (not threads) to bypass the GIL. Ideal for CPU-bound tasks like data processing, image manipulation, and scientific computing.
15 min•By Priygop Team•Updated 2026
Multiprocessing
Multiprocessing
from concurrent.futures import ProcessPoolExecutor, ThreadPoolExecutor
import time
# CPU-bound task
def compute(n):
"""Simulate heavy computation."""
total = sum(i * i for i in range(n))
return total
# Sequential
start = time.time()
results = [compute(100_000) for _ in range(4)]
print(f"Sequential: {time.time() - start:.3f}s")
# With ThreadPool (limited by GIL for CPU)
start = time.time()
with ThreadPoolExecutor(max_workers=4) as executor:
results = list(executor.map(compute, [100_000] * 4))
print(f"ThreadPool: {time.time() - start:.3f}s")
# When to use what:
print("\n=== Threading vs Multiprocessing ===")
print("Threading → I/O-bound → network, file, database")
print("Multiprocessing → CPU-bound → math, image, data")
print("asyncio → I/O-bound → many concurrent connections")
# ProcessPoolExecutor (runs in separate processes)
# start = time.time()
# with ProcessPoolExecutor(max_workers=4) as executor:
# results = list(executor.map(compute, [100_000] * 4))
# print(f"ProcessPool: {time.time() - start:.3f}s")
# Note: ProcessPoolExecutor requires __name__ == '__main__'
# guard and cannot be demonstrated in some environmentsTip
Tip
Use asyncio for high-concurrency I/O: web servers, chat apps, API clients. It handles thousands of connections on a single thread.
Diagram
Loading diagram…
asyncio for I/O. multiprocessing for CPU.
Common Mistake
Warning
Mixing sync and async code incorrectly. Don't call blocking functions in async code — use await or run_in_executor.
Quick Quiz
Practice Task
Note
(1) Write an async function with asyncio.sleep. (2) Run multiple async tasks with gather(). (3) Create an async context manager.