Global Interpreter Lock (GIL) in Python

Shivam Dhanadhya
4 min readSep 17, 2021

--

Recalling Process/Thread/Locking Quickly:

Before we try to understand the concept of global interpreter lock in Python it is important to revise some of the concepts which are specifically associated with process/threat level execution and locking mechanism.

Multiprocessing system ?

A system that can execute multiple processes at a time, either the system may achieve it by using multiple CPUs (execution units) or multiple cores in a single CPU is known as a multi-processing system.

Multiprogramming system ?

Multiprogramming systems can run multiple application programs by using the technique of context switching.

Multitasking system ?

Multitasking is a somewhat specialized version of a multiprogramming system, in which there are multiple tasks getting executed on a given CPU or on a given CPU core (obviously one at a time), this type of system is known as a multitasking system.

What is Multithreading ?

Multithreading can be thought of as the specialized version of multitasking wherein the CPU switches between multiple threads of the same given process whereas in the case of multitasking CPU switches between different tasks. mechanism wise multithreading is similar to that of multitasking, but multithreading is taking place in the context of the same process, not multiple processes.

What is Mutex ?

Mutex is nothing but the locking mechanism which decides to which specific process or thread the resource to be allocated. As part of mutex-based locking, only one process can acquire a mutex at a time and hence that single process gets access to the resource.

Global Interpreter Lock in Python 2:

Global Interpreter Lock is nothing but a mutex based locking mechanism used by the Python interpreter itself for executing threads in multithreaded fashion.

Basically, global interpreter lock makes sure that a single thread which is holding the mutex lock only gets access two the resource (CPU).

And the provision of keeping mutex over there in Python was mandatory because the memory management in Python is not locking safe, hence given more than one threats a chance to access the memory the data stored inside memory might not be synchronously updated, hence the need of GIL.

Global Interpreter Lock in Python 2:

As from the above explanation for GIL we clearly got an idea that global interpreter lock is important to provide mutex locking mechanism over there, still Python claims that it supports multithreading. let’s see how exactly it happens in case of Python 2:

whenever there are having multiple threads to be executed by utilizing the allocated CPU resource, the question arises on which basis the thread switching should be performed so that each and every thread waiting to be executed gets hold on mutex lock and continues its processing on a CPU.

So specifically in Python 2, GIL follows a technique of waiting for 100 ticks to be completed on a CPU by the given thread.

A single tick stands for a single byte level instruction, once thread-one is done with such 100 byte-level instructions, a signal would be generated by the ongoing thread that it wants to relieve the mutex lock and any other from rest of the threads can acquire the released mutex lock now. Next thread which acquires the mutex lock now gets a chance to execute unless it completes its own 100 ticks of execution and so on and so forth.

Global Interpreter Lock in Python 3:

GIL follows different mechanism in case of Python 3 as compared to Python 2. As we saw in the earlier section that in case of Python 2, GIL has a signaling mechanism i.e., the signal is generated whenever the ongoing thread has executed for 100 ticks using the CPU resource and once this signal is generated rest of the threads which are waiting to get their own 100 ticks of CPU cycles executed will compete to acquire the mutex lock. This signaling mechanism the overhead of maintenance and also creates a race condition for threads which are willing to acquire the mutex lock.

Also, the mechanism of providing hundred ticks to each of the threads is not full proof and there has to have some polling mechanism to make sure that context is switched between the threads once that specific thread has completed its own execution for given 100 ticks.

Hence to avoid this bottleneck, which is posed by GIL in Python 2, the mechanism of GIL has been modified in Python version 3 wherein GIL provides hold of the mutex lock to the competing threads in a round Robin fashion. A dedicated CPU cycle of 5 millisecond is given to the ongoing thread and then GIL lets another thread to hold the mutex lock and let it execute for next 5 milliseconds, so on and so forth.

So as you can see, here we have talked about the global interpreter lock (GIL) in Python which mutex based locking mechanism. In the initial section, we have recalled some of the process/thread/locking concepts. And then we jumped onto discussion for global interpreter lock in details and working of the same. We discuss the working details of GIL in Python two and Python 3 respectively.

Thanks for reading the blog, keep connected for more such blogs by me (Shivam Dhanadhya). Please feel free to reach out me at https://shivamsdhanadhya.github.io/

--

--

Shivam Dhanadhya
Shivam Dhanadhya

Written by Shivam Dhanadhya

Software Engineer | GT Graduate | Python | Machine Learning | Data Science

No responses yet