Python › Concurrency (asyncio/threading/multiprocessing)
How does multiprocessing.Pool let you parallelize a CPU-bound function across multiple worker processes?
IntermediateAnswer
Pool.map() (or apply_async() for more control) distributes calls of a function across a pool of worker PROCESSES (each with its own Python interpreter, bypassing the GIL entirely), running them in true parallel on separate CPU cores, and collects the results back in the main process.
from multiprocessing import Pool
def square(n):
return n * n
if __name__ == "__main__":
with Pool(processes=4) as pool:
results = pool.map(square, range(10))
print(results) # [0, 1, 4, 9, ..., 81], computed across 4 parallel processesReal-world example Parallelizing a CPU-intensive batch computation (like image resizing or numerical simulation) across all available CPU cores.