Skip to content
BytePatterns

Thread Pools

Concurrency: lesson 5 of 10

Hire the workers once, reuse them all day.

Lesson 5 of 10 · 4 min

Thread Pools

Step 1 of 9

Two workers, created once. Four pages wait in the queue — not four threads.

The Idea

A thread pool keeps a fixed crew of workers alive and feeds them tasks from a queue. Threads are created once, not per task, and the pool size is a deliberate cap on how much work runs at once.

Real-World Example

A ceramics studio owns three kilns. Pots queue on the shelf and go in as kilns free up; the kilns are never demolished after a firing and never rebuilt for the next one. Hiring a fourth potter does not fire a single extra pot.

The Code

from concurrent.futures import ThreadPoolExecutor

def fetch_size(page):
    return page, len(page) * 100      # stands in for a slow network call

pages = ["index", "about", "pricing", "faq"]

with ThreadPoolExecutor(max_workers=2) as pool:
    # two workers, four tasks; map hands results back in input order
    for page, size in pool.map(fetch_size, pages):
        print(page, size)

Your turn

What does this print?

from concurrent.futures import ThreadPoolExecutor

def double(n):
  return n * 2

with ThreadPoolExecutor(max_workers=3) as pool:
  out = list(pool.map(double, [1, 2, 3, 4]))

print(out)

Mini quiz

1 / 3

A pool exists mainly to: