Threads, processes or asyncio: which fits each of these jobs?
Question
A Python 3.12 service (standard CPython) has three jobs. For each one, pick threads, processes or asyncio, and say why.
- Score 1 million records with a pure-Python function: loops and arithmetic, no I/O.
- Download 200 web pages with a blocking HTTP client such as
requests. - Keep 10,000 WebSocket connections open. Clients are idle most of the time.
Show hints (3)
- "WHERE THE TIME GOES"
Is each job busy computing, or mostly waiting for the network? The right tool depends on that.
- "ONE AT A TIME"
In standard CPython only one thread runs Python code at a time. But a thread that waits on the network lets the others run. Which jobs does that help?
- "COST PER CONNECTION"
Each thread is a real OS thread with its own stack. An
asynciotask is a small Python object, and thousands of them share one thread. Which fits 10,000 idle connections?
Show checked explanation
Job 1: processes. In standard CPython the GIL lets only one thread run Python code at a time, so threads don't speed up pure-Python work. Job 2: threads. A thread releases the GIL while it waits on the network. Job 3: asyncio. One thread can serve thousands of idle connections cheaply.
from concurrent.futures import ProcessPoolExecutor def score(n): """CPU-bound: pure Python arithmetic, no I/O.""" return sum(i * i for i in range(n)) if __name__ == "__main__": sizes = [1_000, 2_000, 3_000] with ProcessPoolExecutor(max_workers=3) as pool: print(list(pool.map(score, sizes)))
[332833500, 2664667000, 8995500500]
Code run on CPython 3.12 against the examples above. Checked by an editor other than the author.
Walkthrough
First ask where each job spends its time: computing (CPU-bound) or waiting (I/O-bound).
| job | time goes to | pick | why |
|---|---|---|---|
| 1. score records | computing in Python | processes | each process has its own GIL |
| 2. 200 downloads | waiting for the network | threads | a waiting thread releases the GIL |
| 3. 10,000 sockets | waiting, mostly idle | asyncio | one thread, one cheap task per connection |
Job 1. The GIL (global interpreter lock) lets only the thread that holds it run Python bytecode. On an 8-core machine, 8 threads running this loop still use about one core. 8 processes each have their own GIL, so they can keep all 8 cores busy. The cost: inputs and results are pickled and copied between processes. Threads speed up CPU work only when it runs in C code that releases the GIL, such as hashlib on large data.
Job 2. requests.get mostly waits for bytes, and a waiting thread releases the GIL. So 20 threads can keep 20 downloads going at once. With an async HTTP client, asyncio works too.
Job 3. 10,000 threads would each need a stack and OS scheduling. asyncio runs all connections in one thread, as coroutines that pause at await and let the others run. This works only if nothing blocks the loop.
The code above runs job 1 in a process pool. For job 2, swap in ThreadPoolExecutor; the rest stays the same.
Common mistakes
- Using threads to speed up pure-Python computing. Under the GIL the threads take turns, so the job takes about as long as with one thread, or longer.
- Calling a blocking function such as
time.sleeporrequests.getinsideasync def. It freezes the event loop and every other task. Use an async library, orawait asyncio.to_thread(...). - Leaving out the
if __name__ == "__main__":guard. On Windows and macOS each worker imports your main module, runs the unguarded pool code again, and the pool breaks. - Sending big objects to a process pool for tiny tasks. Pickling and copying them can cost more than the work. Send small inputs, and give each task more work.
Follow-up questions
- "Doesn't the GIL make my threaded code thread-safe?" No. The GIL protects the interpreter's own data, not your logic.
counter += 1is a read, an add and a write, and Python doesn't promise to run them as one step. Two threads can then lose an update. Protect shared state with athreading.Lock. - "Your
asyncioserver also needs to do some CPU-heavy work. How?" Keep the event loop for I/O and send the CPU work to a process pool:await loop.run_in_executor(pool, score, n), wherepoolis aProcessPoolExecutor. The loop keeps serving other connections while the work runs in another process. - "What about free-threaded Python?" Python 3.13 added a separate free-threaded build without the GIL, as an experimental feature (PEP 703). Python 3.14 made it officially supported but still optional (PEP 779). The default build still has the GIL. Without the GIL, threads run Python code in parallel, but a C extension that doesn't support it can turn the GIL back on.
What a strong answer covers
Sorts each job into CPU-bound or I/O-bound before naming a tool, and explains the GIL in one sentence. Knows threads still help with I/O, warns about blocking calls in async code, and knows the free-threaded build is optional.
Open in interactive practice deck →