Part 13 / Topic 4 of 11
The GIL
The GIL (Global Interpreter Lock) is a lock inside the normal CPython interpreter. It allows only one thread to run Python code at a time. A thread that is waiting (sleeping, reading a file, waiting for the network) lets go of the GIL, so others can run. Like a kitchen with only one knife: four cooks can each wait for their own pot, but only one can chop at a time.
So, on the normal build:
| Work | Do threads help? | Why |
|---|---|---|
| I/O-bound | ✅ Yes | While one thread waits, the GIL is free for the others |
| CPU-bound | ❌ No | The threads take turns with the GIL; no two calculate at once |
import sys
# sys._is_gil_enabled() only exists on Python 3.13+, so check first
if hasattr(sys, "_is_gil_enabled"):
print("GIL enabled:", sys._is_gil_enabled())
else:
print("GIL enabled: True (Python older than 3.13)")
# GIL enabled: True (on the normal build)
The free-threaded build
Python is slowly removing the GIL:
- Python 3.13 added an experimental free-threaded build without the GIL. It is a separate download, usually started as
python3.13t. - Python 3.14 made the free-threaded build officially supported (PEP 779), but it is still not the default. The normal
pythonstill has the GIL.
On the free-threaded build, the code above prints GIL enabled: False, and threads can speed up CPU work. Some third-party packages don't support it yet, so for now: threads for I/O, processes for CPU (the next topic).
🔍 Going deeper: a free-threaded build turns the GIL back on by itself if you import an extension module that isn't marked as safe. You can also force it with
python -X gil=0orPYTHON_GIL=0.
Done here? The practice set for Part 13 — 8 exercises with solutions — is on the part page.