Also known as: Global Interpreter Lock
The Global Interpreter Lock (GIL) is a mutex in the CPython interpreter that prevents multiple threads from executing Python bytecode simultaneously. At any given moment, only one thread holds the GIL and can run Python code; other threads must wait. CPython periodically switches which thread holds the GIL (every 5 milliseconds by default, configurable via sys.setswitchinterval()), giving the illusion of concurrency.
The GIL exists because CPython's memory management (reference counting) is not thread-safe. Without the GIL, simple operations like incrementing a reference count would need atomic instructions or locks, adding overhead to every single object operation. The GIL is a pragmatic engineering trade-off: it simplifies CPython's internals and C extension development at the cost of limiting multi-threaded CPU-bound performance.
The GIL does not affect I/O-bound threads (it is released during I/O operations) or multi-process parallelism (each process has its own GIL). For CPU-bound work, the standard workaround is multiprocessing or concurrent.futures.ProcessPoolExecutor. PEP 703 proposes a "free-threaded" CPython build that removes the GIL entirely; experimental builds are available from Python 3.13, with the feature expected to mature over several releases.
Discussed in:
- Chapter 19: Concurrency and Parallelism — The GIL