|
| 1 | +""" |
| 2 | +Process-based parallelism via multiprocessing and concurrent.futures. |
| 3 | +
|
| 4 | +While threads share memory and are constrained by CPython's Global Interpreter |
| 5 | +Lock (GIL) for CPU-bound tasks, multiprocessing spawns independent operating |
| 6 | +system processes. Each process has its own memory space and Python interpreter, |
| 7 | +allowing CPU-intensive work to execute in parallel across multiple CPU cores. |
| 8 | +
|
| 9 | +Key patterns demonstrated: |
| 10 | +- `ProcessPoolExecutor`: High-level interface for managing worker processes |
| 11 | +- `os.getpid()`: Demonstrating execution across distinct process IDs |
| 12 | +- `executor.map()`: Batch mapping inputs across worker processes |
| 13 | +- `executor.submit()`: Fine-grained task submission with `Future` objects |
| 14 | +- Exception propagation across process boundaries |
| 15 | +""" |
| 16 | + |
| 17 | +import os |
| 18 | +from concurrent.futures import ProcessPoolExecutor |
| 19 | + |
| 20 | + |
| 21 | +def compute_square(n: int) -> int: |
| 22 | + """CPU-bound task to square a number.""" |
| 23 | + return n * n |
| 24 | + |
| 25 | + |
| 26 | +def get_worker_pid(_: int) -> int: |
| 27 | + """Return the operating system process ID of the worker.""" |
| 28 | + return os.getpid() |
| 29 | + |
| 30 | + |
| 31 | +def failing_task(n: int) -> int: |
| 32 | + """Task that raises an exception to demonstrate inter-process error propagation.""" |
| 33 | + if n < 0: |
| 34 | + raise ValueError("Input must be non-negative") |
| 35 | + return n |
| 36 | + |
| 37 | + |
| 38 | +def main() -> None: |
| 39 | + main_pid = os.getpid() |
| 40 | + |
| 41 | + # 1. Direct execution establishes baseline behavior in the main process |
| 42 | + assert compute_square(3) == 9 |
| 43 | + assert get_worker_pid(0) == main_pid |
| 44 | + assert failing_task(1) == 1 |
| 45 | + direct_fail = False |
| 46 | + try: |
| 47 | + failing_task(-1) |
| 48 | + except ValueError: |
| 49 | + direct_fail = True |
| 50 | + assert direct_fail is True |
| 51 | + |
| 52 | + # 2. Using ProcessPoolExecutor with a controlled pool size |
| 53 | + with ProcessPoolExecutor(max_workers=2) as executor: |
| 54 | + # 3. executor.map processes inputs in parallel across worker processes |
| 55 | + numbers = [1, 2, 3, 4, 5] |
| 56 | + squares = list(executor.map(compute_square, numbers)) |
| 57 | + assert squares == [1, 4, 9, 16, 25] |
| 58 | + |
| 59 | + # 3. Prove execution occurs in separate process IDs |
| 60 | + worker_pids = set(executor.map(get_worker_pid, range(4))) |
| 61 | + # Worker processes have their own PIDs, distinct from the main process |
| 62 | + assert main_pid not in worker_pids |
| 63 | + assert len(worker_pids) > 0 |
| 64 | + |
| 65 | + # 4. executor.submit for individual asynchronous tasks |
| 66 | + future = executor.submit(compute_square, 12) |
| 67 | + assert future.result() == 144 |
| 68 | + |
| 69 | + # 5. Exceptions raised in child processes propagate to future.result() |
| 70 | + failing_future = executor.submit(failing_task, -5) |
| 71 | + task_failed = False |
| 72 | + try: |
| 73 | + failing_future.result() |
| 74 | + except ValueError as e: |
| 75 | + task_failed = True |
| 76 | + assert str(e) == "Input must be non-negative" |
| 77 | + assert task_failed is True |
| 78 | + |
| 79 | + |
| 80 | +if __name__ == "__main__": |
| 81 | + main() |
0 commit comments