/ˈɡloʊbəl ɪnˈtɜːrprɪtər lɒk/

noun … “A single-thread lock for memory safety in Python.”

Global Interpreter Lock, commonly abbreviated as GIL, is a mutex used in the CPython implementation of Python to ensure that only one thread executes Python bytecode at a time within a single process. The primary purpose of the GIL is to protect access to Python objects, preventing data corruption caused by concurrent modifications and simplifying memory management, especially in reference counting-based garbage collection.

The GIL works by allowing a single thread to acquire execution rights over the interpreter while other threads are blocked from running Python code. When the active thread reaches a blocking operation, such as I/O, or after a fixed time slice, the GIL is released and another thread can acquire it. This mechanism ensures memory safety but introduces a limitation: CPU-bound Python threads cannot execute in true parallelism within a single process. Multi-core parallelism is typically achieved using multiprocessing or by offloading compute-intensive tasks to native extensions written in C or C++ that release the GIL.

Key characteristics of the GIL include:

  • Thread serialization: only one thread executes Python bytecode at a time.
  • Memory safety: prevents race conditions on internal Python objects.
  • Simplified reference counting: ensures that object allocation and deallocation remain consistent without complex locking.
  • Performance tradeoff: limits CPU-bound thread parallelism while allowing I/O-bound concurrency.

In practical usage, a Python developer writing a multithreaded server may notice that threads handling heavy computational tasks do not achieve linear speedup across multiple CPU cores due to the GIL. However, threads waiting on network responses or disk I/O can still run efficiently, as the GIL is released during blocking calls. For CPU-intensive processing, developers often use multiprocessing to spawn separate Python processes, each with its own GIL instance, achieving parallel execution.

Conceptually, the GIL acts like a single-lane bridge over a river. Only one car (thread) can cross at a time, preventing collisions (data corruption) but potentially creating a bottleneck if multiple cars attempt to cross simultaneously. For lightweight, I/O-focused traffic, the bridge works fine, but for heavy computational traffic, traffic must be rerouted via multiple bridges (processes) to maintain throughput.

See Python, Interpreter, Threading, Multiprocessing.