← Back to list

What is GIL in Python?

With the advancement in AI, building agents, it has become extremely important to understand the fundamental concepts like GIL to build…

Shravani · 2026-01-18 15:39 · 14 claps · 2.8 min read
#python #global-interpreter-lock #mutex #multithreading #threads
Open on Medium ↗
Wiki topics: AGT · AI Agents 📚 · Books & Reading

What is GIL in Python?

With the advancement in AI, building agents, it has become extremely important to understand the fundamental concepts like GIL to build efficient systems powered by AI

Global Interpreter Lock (GIL)

If you are using python to build your backend applications, ML model training, data pipelines, etc, you must have definitely come across the word GIL or at least multithreading.

Before understanding what GIL is, let’s understand what a Interpreter is.

Interpreter in python is a program that takes your code, checks it for errors, translates it to machine understandable format called bytecode and sends it to the Python Virtual Machine (PVM) for execution.

So, every time you run a python script or you start a web server written in Python, you are invoking the interpreter.

GIL is a lock (or a mutex) on the Python Interpreter that allows only one thread to hold the control on it.

In other words, only a single thread can be in execution at any given point of time.

Why does GIL exist in Python?

Now that we understand what GIL is, it is important to understand why it was introduced.

If you have ever heard of the term garbage collection, it is an automatic memory management process which removes objects that are no longer in use and frees up the memory occupied by that object.

Python uses a concept called reference count for Garbage collection. It created a reference count variable for every object and uses this variable to keep track of the number of references to that object. When the number drops to zero, it removes the object and frees the memory occupied.

If GIL wasn’t there and multiple threads could run simultaneously, the reference variable count in each object could be modified simultaneously by multiple threads which would result in either a leaked memory or worst case deletion of object from memory while its still in use.

You could ask, why not use the lock for reference count variable in every object?

As nice and simple as that sounds, there are huge tradeoffs with this approach. Having a lock on the variable, means every time a thread needs access to an object, it should acquire the lock and since multiple objects interact with each other, there is a chance of deadlock. Creating and acquiring locks often can also lead to performance degradation.

What is the impact of GIL on Multithreading?

GIL has no effect in single thread applications. But when it comes to Multithreaded applications, since GIL forces only thread to execute the bytecode even with the presence of multiple CPU cores, it is a bottleneck for CPU bound operations.

CPU bound operations are those which require CPU computations like matrix multiplications, image processing, etc.

If you are doing some heavy model training without using multiprocessing or libraries with c extensions or CUDA code, you will see a huge impact on the training time due to GIL.

When it comes to I/O bound operations, Python’s GIL is designed to be release when a thread is waiting for an I/O operation.

I/O operations are the ones that spend time on data transfer which can come from user input, file, network, etc.

This feature is what made frameworks like FastAPI use asynchronous programming to out perform frameworks like Flask.

Does Python have interpreters without GIL?

The concept of GIL exists in the standard python interpreter “CPython”.

To check which python interpreter you are using, you can run the below code,

import sys
print(sys.implementation.name)

There are other interpreters in different Python implementations which don’t use GIL, some of them are Jython, PyPy, IronPython, etc which can be used.

The newer version of Standard implementation of Python (3.13+)with CPython, provides experimental binaries that you can try with gil disabled. Read more about it ***here***.

Why is to hard to remove GIL from Python?

One of the biggest reasons it is hard to remove GIL completely is because of the impact it would have on the c extensions.

Python was originally created to be a language which was easier to code, read, well integrated with c libraries. So, the GIL also provides a support for c extensions which are not thread safe. Removing GIL completely would mean a lot of backward compatability issues with c extensions.

Read more about the experiments done to remove the GIL from the Python creator himself ***here.***


메타데이터
post_id
83d612b7ece4
slug
what-is-gil-in-python-83d612b7ece4
url
https://medium.com/@ravinuthalashravani/what-is-gil-in-python-83d612b7ece4
canonical_url
https://medium.com/@ravinuthalashravani/what-is-gil-in-python-83d612b7ece4
author_url
https://medium.com/@ravinuthalashravani
status
ok
fetched_at
2026-06-26 12:24:55