Sub topic 5
What the GIL is and what it protects
This chapter assumes you can write Python functions and use with statements. It also assumes you have started a thread with threading.Thread. Everything here describes CPython, the standard Python implementation.
What the GIL is
The Global Interpreter Lock (GIL) is a mutex inside CPython. A mutex is a lock that only one owner can hold at a time. A thread must hold the GIL to run Python bytecode, which is the instruction format the interpreter executes. So in a standard build, only one thread executes Python code at any moment, even on a machine with many cores.
Threads still take turns. A running thread gives up the GIL in two situations:
- It blocks on something outside Python, such as
time.sleep, a socket read, or a file read. - It has held the GIL for the switch interval, which is 5 milliseconds by default, and another thread is waiting.
You can inspect these settings from Python:
import sys
# sys._is_gil_enabled() exists in Python 3.13 and later; older versions always have a GIL.
gil_enabled = getattr(sys, "_is_gil_enabled", lambda: True)()
print("GIL enabled:", gil_enabled)
print("Switch interval (seconds):", sys.getswitchinterval())On a standard build this prints GIL enabled: True and 0.005.
What the GIL protects
The GIL protects the interpreter's own internal state. The main example is reference counting. Every object stores a count of how many references point to it, and CPython frees the object when the count reaches zero. If two threads changed a count at the same time without coordination, the count could end up wrong. The object could be freed while still in use, or never freed. One global lock makes these updates safe without a lock on every object.
The GIL also means that a single built-in operation, such as appending to a list, cannot corrupt the list's internal structure when threads call it at the same time.
What the GIL does not protect
The GIL does not make your code thread-safe. The interpreter can switch threads between bytecode instructions, and a single line of your code often compiles to several instructions. You can see this with the dis module:
import dis
def increment(counter):
counter["n"] += 1
dis.dis(increment)The listing shows separate steps for loading the value, adding one, and storing the result. A thread switch between the load and the store lets another thread's update be overwritten. That is a race condition, and the GIL does not prevent it. You still need a threading.Lock around shared state that several threads modify. The previous chapter showed this with the counter example.
Why CPython has it
The GIL is a trade-off. It keeps single-threaded programs fast, because there is no per-object locking cost. It also makes it simple to write C extensions, because extension code can assume the interpreter state is not changing under it. The cost is that pure-Python CPU-bound code cannot use several cores through threads. The next chapter looks at that cost in detail.
Exercise
Two threads each run total += 1 100,000 times on a shared global, with no lock, on a standard CPython build. The result is sometimes below 200,000. If the GIL allows only one thread to run bytecode at a time, why does this happen?
Show answer
The GIL serializes individual bytecode instructions, not whole lines of your code. total += 1 compiles to several instructions: load, add, store. The interpreter can switch threads between them, so one thread can overwrite the other's update. A threading.Lock around the increment makes the whole operation exclusive.