What is the Python Global Interpreter Lock (GIL)

PythonServer Side ProgrammingProgramming

In this article, we will learn about What is the Python Global Interpreter Lock (GIL).

This is a lock or hindrance that resistant the availability of the Python interpreter to multiple threads simultaneously. GIL is identified as a fault/issue in Python 3.x. Or earlier as it doesn’t allow multithreading in a multi-threaded architecture.

Why is it introduced?

Python supports the concept of automatic garbage collection. As soon as the reference count of an object reaches zero the memory is cleaned and free for usage.

>>> import sys
>>> var = {}
>>> print(sys.getrefcount(ar))
>>> 2
>>> v=var
>>> print(sys.getrefcount(v))
>>> 3

Of in this case if the reference count starts decreasing and increasing simuktaneiously the automatic collector won’t be able to work properly and hence chances of memory leakage are increased.

To reduce this risk the GIL was introduced in python. By adding global lock is considered to be better than adding the lock to each variable that would result in a series of locks leading to deadlocking.

Why the GIL still exists in Python

GIL needs to be improved so that we can handle it in a better manner. So instead of removing GIL, we are working on improving the concept of GIL. as python is purely linked with the base of c and cpython we can’t directly remove the Gil. Although there are various methods to handle the issues that GIL which solves are difficult to implement and decreasing the processing and run time of the system.

For e.g.,

Let us suppose process P1 is going on having threads t1 and t2. Python threads being native by nature, are scheduled by the underlying operating system.

t1 running(run stage) (acquire GIL) → t1 waiting for I/O(in/out) (releases GIL) → t2 running(run stage) (acquires GIL, by this time t1 is also ready to run but GIL is acquired by t2)

Hence, here the GIL becomes the major restriction. SO, if we want to write a multi-threaded python application/module that does heavy CPU bound operations, it becomes impossible to get the desired results.

Because, in Python real multi-threading is not possible, as effectively the process will be utilizing only one CPU at a single time even when multicore CPU is available.

However, in most Python applications (web application, Django based servers, etc.), this doesn't remain an issue as these applications are I/O bounded by nature.

As a Python programmer, we never have to deal with acquiring and releasing GIL until we are writing C/C++ modules/scripts that are executable in python


In this article, we learnt about the Python Global interpreter lock, its importance & why can’t be removed directly from python.

Updated on 28-Aug-2019 13:52:08