Robots Atlas>ROBOTS ATLAS

Python — From Basics to Advanced · Concurrency II — Threads, Processes, GIL

GIL — What It Is and Why It Matters

Concurrency II — Threads, Processes, GIL

Introduction

The GIL — Global Interpreter Lock — is a mutex in CPython that ensures only ONE thread executes Python bytecode at any moment. This is NOT a language feature — it is a CPython implementation detail (PyPy has its own GIL, Jython and IronPython do not have one). The GIL simplifies memory management (reference counting without atomic ops on every access) at the cost of true parallelism in pure Python.

The GIL is released during blocking I/O operations (read/write, socket, sleep), C-extension calls marked with Py_BEGIN_ALLOW_THREADS (e.g., NumPy, requests, file I/O), and every sys.setswitchinterval (default 5ms). That is why threading in CPython IS faster for I/O-bound tasks but provides NO speedup for CPU-bound work — you need multiprocessing or C-extensions for that.

PEP 703 (Python 3.13+) introduces an optional free-threaded build (python3.13t) — without the GIL. The cost is ~10% single-threaded overhead and a requirement that C-extensions be thread-safe (opt-in flag). This is evolution, not revolution: the GIL remains the default through 3.16+.