Multiprocessing Basics
Introduction to Multiprocessing in Python
Python's multiprocessing module provides a powerful way to leverage multiple CPU cores and execute tasks concurrently. Unlike threading, multiprocessing truly runs processes in parallel, bypassing the Global Interpreter Lock (GIL) and enabling genuine parallel computation.
Core Concepts
Process Creation
In multiprocessing, you can create multiple processes that run independently and simultaneously. Each process has its own memory space and Python interpreter.
from multiprocessing import Process
def worker(name):
print(f"Worker process: {name}")
if __name__ == '__main__':
processes = []
for i in range(3):
p = Process(target=worker, args=(f"Process-{i}",))
processes.append(p)
p.start()
for p in processes:
p.join()
Process Pool
Process pools allow you to manage a group of worker processes efficiently:
from multiprocessing import Pool
def square(x):
return x * x
if __name__ == '__main__':
with Pool(processes=4) as pool:
results = pool.map(square, [1, 2, 3, 4, 5])
print(results)
Key Characteristics
Feature |
Description |
Parallel Execution |
Runs tasks simultaneously on multiple CPU cores |
Independent Memory |
Each process has isolated memory space |
Inter-Process Communication |
Supports various communication mechanisms |
Workflow of Multiprocessing
graph TD
A[Main Program] --> B[Create Processes]
B --> C[Start Processes]
C --> D[Execute Tasks]
D --> E[Collect Results]
E --> F[Terminate Processes]
Best Practices
- Use
if __name__ == '__main__':
to prevent recursive process creation
- Close and join processes after use
- Be mindful of memory overhead
- Use process pools for better resource management
When to Use Multiprocessing
- CPU-bound tasks
- Computational intensive operations
- Parallel data processing
- Leveraging multi-core processors
At LabEx, we recommend understanding multiprocessing fundamentals before diving into advanced task cancellation techniques.