December 5, 2019

GIL - Global Interpretation Lock (Python/Ruby)

Sources:


GIL works well for:

  • single-threaded programs
  • multi-threaded I/O-bound programs

GIL has disadvantages for:

  • multi-threaded CPU-bound programs

In the multi-threaded version the GIL prevented the CPU-bound threads from executing in parellel. The GIL does not have much impact on the performance of I/O-bound multi-threaded programs as the lock is shared between threads while they are waiting for I/O.

GIL is still hasn’t removed from CPython (origin Python implementation), including Python 3. There is only one reason:

Python community don’t want performance decrease in single-threaded programs.

The creator and BDFL of Python, Guido van Rossum, gave an answer to the community in September 2007 in his article “It isn’t Easy to remove the GIL”:

“I’d welcome a set of patches into Py3k only if the performance for a single-threaded program (and for a multi-threaded but I/O-bound program) does not decrease


While many Python users take advantage of the single-threaded performance benefits of GIL, you can get around single-threaded GIL using multi-processing:

The most popular way is to use a multi-processing approach where you use multiple processes instead of threads. Each Python process gets its own Python interpreter and memory space so the GIL won’t be a problem.

However, multiple processes are heavier than multiple threads.