Skip to content

Python GIL ​

Ref: Grok the GIL: How to write fast and thread-safe Python

One thread runs Python, while N others sleep or await I/O. -- Grok the GIL

In CPython, the GIL is just a lock:

c
static PyThread_type_lock interpreter_lock = 0; /* This is the GIL */
c
void PyEval_InitThreads(void)
{
    interpreter_lock = PyThread_allocate_lock();
    PyThread_acquire_lock(interpreter_lock);
}

Python 2 vs Python 3 ​

When a thread gives up the GIL:

  • Python 2: after 1000 bytecode instructions
  • Python 3: after 15 ms

頁面歷史

Just observe 👀