Skip to content

Process Pool (Pool, map, starmap)

Creating many processes manually is not ideal.

A pool:

  • reuses worker processes
  • makes parallel execution easier
pool_map.py
from multiprocessing import Pool
 
 
def square(x: int) -> int:
    return x * x
 
 
if __name__ == "__main__":
    with Pool(processes=4) as pool:
        results = pool.map(square, [1, 2, 3, 4, 5])
 
    print(results)
pool_starmap.py
from multiprocessing import Pool
 
 
def power(x: int, p: int) -> int:
    return x ** p
 
 
if __name__ == "__main__":
    with Pool(processes=2) as pool:
        results = pool.starmap(power, [(2, 3), (3, 2), (4, 2)])
 
    print(results)
sketch Threads make CPU-bound Python slower; processes make it faster, but not 4x p5.js
Eight jobs, each a tight arithmetic loop, on an 8-core machine. Threads lose to plain sequential code because the GIL lets only one run Python bytecode at a time and you pay for the switching anyway. A process pool genuinely parallelises, but four workers do not give four times the speed: spawning a process re-imports your module, and every argument and result is pickled across a pipe. These are measured wall-clock times, not estimates.
  • Forgetting the if __name__ == "__main__" guard.
  • Passing non-picklable objects (like lambdas on some platforms).

pch.coffeeTagline

pch.coffeeCta

pch.feedbackHeading

pch.feedbackSubheading