Sub topic 1
What the GIL is and why CPython has one
Prerequisites
This chapter assumes you can read basic Python and have used the threading module or concurrent.futures. You should also know the difference between a process and a thread, and have a rough idea of what a race condition and a lock are. No C knowledge is needed.
What the GIL is
The Global Interpreter Lock (GIL) is a mutex inside the standard CPython interpreter. A mutex is a lock that only one thread can hold at a time. A thread must hold the GIL to execute Python bytecode, so only one thread runs Python code at any moment, even on a machine with many cores.
The GIL is a detail of CPython, the reference implementation. It is not part of the Python language. Other implementations make different choices.
Why CPython has one
CPython tracks the lifetime of every object with reference counting. Each object stores a count of the references that point to it. When the count drops to zero, the interpreter frees the object.
Updating a count is a read-modify-write step. If two threads changed the same count at once, an update could be lost. A count that is too low frees an object that is still in use. A count that is too high leaks memory.
There were two ways to prevent this:
- Put a lock on every object. This costs memory and time, and it risks deadlocks.
- Put one lock around the whole interpreter. This is simple and fast for a single thread.
CPython chose the second. The GIL also protects other internal state, such as the allocator and the data structures of built-in types. It made writing C extensions easier, because extension code could assume that no other thread was touching Python objects while it ran.
How threads take turns
A thread does not hold the GIL forever. The interpreter asks the running thread to release it in two situations:
- A time limit passes. By default a waiting thread requests the GIL after 5 milliseconds. You can read this value with
sys.getswitchinterval(). - The thread blocks on I/O. Reading a file, waiting on a socket, or calling
time.sleep()releases the GIL, so other threads can run meanwhile.
You can inspect the switch interval yourself:
import sys
print("Switch interval (seconds):", sys.getswitchinterval())On a default build this prints 0.005.
What the GIL does and does not protect
The GIL keeps the interpreter's own data consistent. Because of it, a single operation such as list.append() on one list cannot corrupt the list's internal structure.
It does not make your program thread-safe. The thread switch can happen between bytecode instructions, and a statement like counter += 1 compiles to several of them: load the value, add one, store the result. Another thread can run in between, and an update is lost.
What this means for performance
Because only one thread runs bytecode at a time, the effect depends on the kind of work:
| Workload | Effect of the GIL |
|---|---|
| I/O-bound (network, disk) | Small. Threads release the GIL while waiting, so they overlap well. |
| CPU-bound pure Python | Large. Threads take turns, so four threads are no faster than one. |
| C extensions that release the GIL (for example parts of NumPy) | Small. Heavy work runs outside the GIL. |
The next chapter looks at the CPU-bound case in detail. Free-threaded Python, the subject of the rest of this tutorial, removes the GIL so that the second row can use multiple cores.
Exercise
A colleague says, "Python has the GIL, so my threaded code can't have race conditions." Give the flaw in this claim, using counter += 1 as your example.
Show answer
The GIL only keeps the interpreter's internal data consistent. counter += 1 is several bytecode instructions (load, add, store), and the interpreter can switch threads between them. Two threads can both read the same old value and both write back the same new value, so one increment is lost. A lock around the update is still required.