Python Multiprocessing
Use multiple CPU cores with multiprocessing.
True parallelism in Python.
Basic Multiprocessing
```python from multiprocessing import Process
def worker(name): print(f"Worker {name} starting...") # CPU-intensive work result = sum([i ** 2 for i in range(1000000)]) print(f"Worker {name} done!")
if __name__ == "__main__": processes = [] for i in range(4): p = Process(target=worker, args=(i,)) p.start() processes.append(p) for p in processes: p.join() ```
Process Pool
```python from multiprocessing import Pool
def square(n): return n ** 2
if __name__ == "__main__": with Pool(4) as pool: results = pool.map(square, range(10)) print(results) # [0, 1, 4, 9, 16, 25, 36, 49, 64, 81] ```
Share Data with Queue
```python from multiprocessing import Process, Queue
def worker(queue, name): queue.put(f"Result from {name}")
if __name__ == "__main__": queue = Queue() processes = [] for i in range(3): p = Process(target=worker, args=(queue, f"Worker{i}")) p.start() processes.append(p) for p in processes: p.join() while not queue.empty(): print(queue.get()) ```
Parallel Processing
```python from multiprocessing import Pool
def process_file(filename): # Process large file with open(filename) as f: return len(f.read())
if __name__ == "__main__": files = ["file1.txt", "file2.txt", "file3.txt"] with Pool() as pool: results = pool.map(process_file, files) print(f"Total size: {sum(results)}") ```
Remember
- Use for CPU-bound tasks - Each process has own memory - Must use if __name__ == "__main__"