multiprocessing Module & Multi-Core CPU Parallelism
1Concept
`multiprocessing` bypasses the GIL entirely by spawning separate operating system processes, each with its own independent Python interpreter and private memory space. It scales CPU-bound computations across 100% of available CPU cores.
2Architecture Diagram
Master Process ---> Fork/Spawn Child Process 1 (Core 0, Private Memory)
---> Fork/Spawn Child Process 2 (Core 1, Private Memory)3Code Example
Python 3.12
import multiprocessing
def square_worker(n: int) -> int:
return n * n
if __name__ == "__main__":
cores = multiprocessing.cpu_count()
print(f"Available CPU Cores: {cores}")
data = [10, 20, 30, 40]
results = [square_worker(x) for x in data]
print(f"Processed Results: {results}")4Expected Output
Available CPU Cores: 8 Processed Results: [100, 400, 900, 1600]
5Key Takeaways
- ✓Processes have separate memory spaces; mutations in one process are invisible to others.
- ✓Spawning processes incurs OS fork/spawn overhead; use process pools for batch jobs.
- ✓Always protect process entry points with `if __name__ == '__main__':` on Windows.