How should you choose threads, processes or asyncio for a slow service?
Python 3.10+; standard library only. GIL discussion refers to conventional CPython builds, not optional free-threaded builds.
Detailed answer
Measure whether the workload waits on I/O or executes Python CPU work. Conventional CPython allows one thread at a time to execute Python bytecode because of the GIL.
Threads suit blocking I/O; asyncio suits cooperative I/O with compatible libraries; processes can parallelize Python CPU work at the cost of serialization and separate memory.
Native extensions may release the GIL. Optional free-threaded CPython changes the assumptions, but application data still needs synchronization. Do not treat concurrency as automatic parallel speedup.
Internal working
The event loop schedules coroutines at await points; a synchronous CPU loop blocks it. Processes communicate by serialized values or explicitly managed shared resources.
Practical scenario
An async API stalls when a handler computes a large prime list. Move CPU work to a bounded process pool and apply request backpressure.
import asyncio
async def job(x):
await asyncio.sleep(0)
return x*x
async def main():
print(await asyncio.gather(job(2), job(3)))
asyncio.run(main())Common mistakes
- Calling blocking libraries inside the event loop.
- Creating an unbounded number of processes.
Key takeaways
- Profile first.
- Bound concurrency.
- Concurrency and parallelism are distinct.
Follow-up challenges
- When does serialization dominate process-pool performance?
- What changes in a free-threaded build?