CodeBlanks Logo Icon CodeBlanks

Pool

Learn process pools with multiprocessing.Pool.

What is multiprocessing.Pool?

multiprocessing.Pool is a pool of processes. It distributes tasks across processes automatically; .map() applies a function to every element of a list.

For example Pool().map(func, [1, 2, 3]) distributes three items across the pool's processes and returns [func(1), func(2), func(3)]. The with Pool() as p: block closes the pool automatically. map preserves input order, so the output is deterministic. This is the most practical way to do parallel mapping.

Parallel computation with map

Pool().map(func, data) applies the function to every element of data. The process count is chosen automatically; usually it equals the CPU core count.

Important: map preserves input order. Input [1, 2, 3] gives output [func(1), func(2), func(3)]; no matter which process finishes first the order stays the same. This ensures deterministic results. If you do not use a with block you must close the pool with p.close() and p.join().

Practice Compute squares with Pool

Import with from multiprocessing import Pool. Define a square function that returns x * x. Inside a with Pool() as p: block assign p.map(square, [1, 2, 3]) to result. Print result.

Starter code

main.py
# write the square function
# compute in parallel with Pool
# print the result

Sample solution

main.py
from multiprocessing import Pool

def square(x):
    return x * x

with Pool() as p:
    result = p.map(square, [1, 2, 3])
print(result)

Expected output

[1, 4, 9]

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 What does Pool().map return?

main.py
with Pool() as p:
    result = p.map(func, [1, 2, 3])
  • A list of results Correct answer
  • A queue
  • A process object
  • A single value

Explanation: answer: a list of results. map lists each item's result in order.

2 Match Pool concepts

Term Meaning
Pool Process pool
map Applies in parallel
with Pool() Auto-closes
func Applied function

3 Complete the Pool map call

Question
with Pool() as p:
    result = p.____(square, [1, 2, 3])
Answer
with Pool() as p:
    result = p.map(square, [1, 2, 3])

Explanation: p.map(func, data) applies the function in parallel.

All lessons in Expert