How to Use multiprocessing Pool map and starmap in Python

Parallelize functions over iterables with Pool.map, and unpack multiple arguments via Pool.starmap.

Medium Python 3.8+ Aug 9, 2026 Concurrency & performance 14 views 0 copies

Python code

20 lines
Python 3.8+
from multiprocessing import Pool


def square(x):
    return x * x


def add_and_multiply(a, b, c):
    return (a + b) * c


if __name__ == "__main__":
    numbers = [1, 2, 3, 4, 5]
    with Pool(processes=2) as pool:
        squares = pool.map(square, numbers)
        print(f"squares: {squares}")

        starmap_args = [(1, 2, 3), (4, 5, 6), (7, 8, 9)]
        results = pool.starmap(add_and_multiply, starmap_args)
        print(f"starmap results: {results}")

Output

stdout
squares: [1, 4, 9, 16, 25]
starmap results: [9, 54, 135]

How it works

Pool.map applies a single-argument function to each item of the iterable, distributing the work across the worker processes. Pool.starmap works like map but unpacks each argument tuple into the function's parameters, allowing multi-argument functions. Using with Pool(...) as pool: ensures the pool is properly closed and joined after the block, preventing leaked processes. The if __name__ == "__main__": guard is required on Windows and recommended everywhere to avoid recursive process spawning. Both methods return results in the same order as the input, blocking until all tasks complete.

Common mistakes

  • Forgetting the `if __name__ == '__main__':` guard on Windows causing infinite recursion.
  • Using `pool.apply` or `pool.map` for multi-argument functions instead of `starmap`.
  • Calling `pool.map` with a generator, which still loads all items into memory.
  • Not using a context manager, risking orphaned pool processes.

Variations

  1. Use `pool.map_async` and `pool.starmap_async` for non-blocking calls and retrieving results later.
  2. Use `Pool(processes=cpu_count())` with `os.cpu_count()` to utilize all cores.

Real-world use cases

  • Processing each row of a large CSV file with a CPU-intensive transformation in parallel.
  • Computing similarity scores for many pairs of vectors where each pair requires multiple parameters.
  • Batching image thumbnail generation across a folder of images using a multi-argument resize function.

Sponsored

Run this sample

Open the browser IDE to tweak the example and see results without installing anything.

Open editor

More from Concurrency & performance

Related tutorials and quizzes for this topic.