The Global Interpreter Lock (GIL) is a mutex (mutual exclusion lock) in computer language interpreters like CPython and Ruby MRI. It permits only one thread at a time to execute Python bytecode within a single process. The GIL's primary purpose is to simplify CPython's memory management and ensure thread safety. Python utilizes reference counting for garbage collection, and the GIL prevents race conditions and memory corruption that could arise from multiple threads modifying object reference counts simultaneously.
While the GIL simplifies memory management and prevents deadlocks, it can be a performance bottleneck for CPU-bound, multithreaded programs. Because of the GIL, multithreaded Python programs cannot fully utilize multi-core processors for parallel execution. However, the GIL has less impact on I/O-bound tasks. To achieve true parallelism, developers can use separate processes, each with its own interpreter and GIL, or explore alternative Python implementations. An experimental "free-threaded" build of CPython was introduced in Python 3.13, which allows Python to be compiled without the GIL.