Python Thread Pool Sizing — ELI5

Imagine you run a pizza restaurant. You have an oven that fits 4 pizzas at once. If you hire 4 cooks, they can each tend a pizza. Hire 20 cooks? Most of them just stand around bumping into each other, and you’re paying all of them. Hire only 1? Pizzas wait in line and customers leave.

Thread pool sizing is figuring out the right number of cooks for your program.

A thread pool is a group of workers your program keeps ready. When a job comes in — download a file, query a database, crunch some numbers — a free worker picks it up. When the job is done, the worker goes back to the pool, ready for the next one.

Too few workers: jobs pile up waiting. Your program is slow even though your computer has power to spare.

Too many workers: workers fight over resources, your computer wastes time switching between them, and memory usage balloons. It’s like having 50 cooks in a tiny kitchen.

The sweet spot depends on what your workers actually do. If they spend most of their time waiting (for web responses, file downloads, database answers), you can have lots of them — maybe 20 or 50 — because waiting doesn’t use the processor. If they’re crunching numbers the whole time, you want roughly one per processor core.

The one thing to remember: the right thread pool size depends on whether your tasks wait a lot (use more threads) or compute a lot (match your CPU core count). Start there, then measure and adjust.

pythonadvancedconcurrency

See Also

  • Python Actor Model Why treating each piece of your program like a person with their own mailbox makes concurrency way less scary.
  • Python Aiocache Caching aiocache remembers expensive answers so your async Python app doesn't waste time asking the same question twice.
  • Python Aiofiles Async Io aiofiles lets your async Python program read and write files without freezing — because normal file operations secretly block everything.
  • Python Aiohttp Understand Aiohttp through an everyday analogy so Python behavior feels intuitive, not random.
  • Python Anyio Portability AnyIO lets your async Python code work with any async library — write once, run on asyncio or Trio without changes.