What is a global interpreter lock (GIL) and why is it an issue?

Technology CommunityCategory: PythonWhat is a global interpreter lock (GIL) and why is it an issue?
VietMX Staff asked 3 years ago
Problem

A lot of noise has been made around removing the GIL from Python, and I’d like to understand why that is so important.

There are several implementations of Python, for example, CPython, IronPython, RPython, etc.

In CPython, the global interpreter lock, or GIL, is a mutex that prevents multiple native threads from executing Python bytecodes at once. This lock is necessary mainly because CPython’s memory management is not thread-safe.

The GIL is controversial because it prevents multithreaded CPython programs from taking full advantage of multiprocessor systems in certain situations. Note that potentially blocking or long-running operations, such as I/O, image processing, and NumPy number crunching, happen outside the GIL. Therefore it is only in multithreaded programs that spend a lot of time inside the GIL, interpreting CPython bytecode, that the GIL becomes a bottleneck.

The GIL is a problem if, and only if, you are doing CPU-intensive work in pure Python. Here you can get cleaner design using processes and message-passing (e.g. mpi4py).