What is Python's GIL, what does it protect, and what are its limitations?
The Global Interpreter Lock (GIL) is a mutex inside CPython that ensures only one thread executes Python bytecode at any given moment. It is not a feature of the Python language itself but an implementation detail of CPython, the reference implementation.
The GIL exists primarily to protect CPython's memory management system. CPython tracks object lifetimes using reference counting -- every object has a refcount field that is incremented when a new reference is created and decremented when one is released. When refcount reaches zero, the object is deallocated. Without the GIL, two threads could simultaneously increment or decrement the same refcount, corrupting it and causing either memory leaks or premature deallocation (use-after-free). Making every refcount operation thread-safe with fine-grained locking would be technically possible but would add significant overhead to every object creation and deletion. The GIL is a coarser, simpler solution.
The practical implications are significant. For CPU-bound workloads -- numerical computation, string processing, pure Python loops -- using threads does not give you parallelism. Multiple threads compete for the GIL, so only one runs at a time. The correct tool for CPU-bound parallelism in Python is the multiprocessing module, which spawns separate processes that each have their own GIL and Python interpreter, achieving true parallelism across CPU cores.
For I/O-bound workloads, threads remain useful. The GIL is released while a thread waits for I/O -- for example, during a network request or disk read. While one thread waits, others can run. This is why frameworks like Django historically used threading for concurrent request handling.
In Python 3.13, CPython added experimental support for running without the GIL (PEP 703, free-threaded mode). This required atomic refcounting and fine-grained locking throughout the interpreter. It is not yet the default and comes with performance trade-offs in single-threaded code.
Candidate knows the GIL prevents true parallelism for CPU-bound code and that I/O-bound threads still benefit, but cannot explain why the GIL exists (reference counting) or what exactly it protects.
Candidate explains reference counting as the root cause, distinguishes CPU-bound vs I/O-bound behavior clearly, describes multiprocessing as the right tool for CPU parallelism, and mentions GIL release during I/O and the direction toward free-threading in Python 3.13.
Reading the answer is step one. Explaining it unprompted — under interview pressure — is what actually matters. Get AI-graded feedback on your answer with follow-up probes on your weak points.
Get Graded — Free Assessment