Advertisement
Intermediate Python, Concurrency (asyncio/threading/multiprocessing)

How does the Global Interpreter Lock (GIL) affect CPU-bound versus I/O-bound multithreaded programs differently?

Short answer

The GIL ensures only ONE thread executes Python bytecode at any given instant, so CPU-bound multithreaded code gets NO real speedup (and can even be slightly slower due to lock overhead); I/O-bound code, however, RELEASES the GIL during blocking operations (like network calls or…

Save Python mock interview
Advertisement
AModel answer
The GIL ensures only ONE thread executes Python bytecode at any given instant, so CPU-bound multithreaded code gets NO real speedup (and can even be slightly slower due to lock overhead); I/O-bound code, however, RELEASES the GIL during blocking operations (like network calls or file reads), so multiple threads CAN make real concurrent progress waiting on I/O.
# CPU-bound: threading gives little to no benefit due to the GIL
def cpu_work(): sum(i * i for i in range(10_000_000))

# I/O-bound: threading helps, since the GIL is released during network waits
def io_work(): requests.get("https://example.com")  # GIL released while waiting
Real-world example
Choosing threading for a multi-URL web scraper (I/O-bound) but multiprocessing for a CPU-heavy data transformation job.
Likely follow-ups
  • How does asyncio compare to threading specifically for I/O-bound concurrency, in terms of overhead?
Related topics