In CPython (default build, GIL enabled), which workload gets a real speedup from threading?
Options
- CPU-bound pure-Python work, like resizing images in a loop
- I/O-bound work, like hundreds of concurrent HTTP requests (Correct option)
- Both kinds of work get the same speedup from extra threads
- Neither kind: extra threads never speed up Python code
Answer
I/O-bound work such as hundreds of concurrent HTTP requests
Why
The GIL (Global Interpreter Lock) is a lock inside CPython that lets only ONE thread run Python code at any moment.
For calculations (CPU work), extra threads just take turns, so the job does not finish faster.
But when a thread waits for the network or the disk, it releases the GIL and another thread can run. With 100 HTTP requests most of the time is waiting, so threads overlap that waiting and the total time drops a lot.
Remember
Threads help when you wait (I/O). For heavy computation, use several processes.