What is the Python Global Interpreter Lock (GIL)
Last Updated :
06 Jan, 2019
Python Global Interpreter Lock (GIL) is a type of process lock which is used by python whenever it deals with processes. Generally, Python only uses only one thread to execute the set of written statements. This means that in python only one thread will be executed at a time. The performance of the single-threaded process and the multi-threaded process will be the same in python and this is because of GIL in python. We can not achieve multithreading in python because we have global interpreter lock which restricts the threads and works as a single thread.
What problem did the GIL solve for Python :
Python has something that no other language has that is a reference counter. With the help of the reference counter, we can count the total number of references that are made internally in python to assign a value to a data object. Due to this counter, we can count the references and when this count reaches to zero the variable or data object will be released automatically. For Example
Python3
# Python program showing
# use of reference counter
import sys
geek_var = "Geek"
print(sys.getrefcount(geek_var))
string_gfg = geek_var
print(sys.getrefcount(string_gfg))
Output:
4
5
This reference counter variable needed to be protected, because sometimes two threads increase or decrease its value simultaneously by doing that it may lead to memory leaked so in order to protect thread we add locks to all data structures that are shared across threads but sometimes by adding locks there exists a multiple locks which lead to another problem that is deadlock. In order to avoid memory leaked and deadlocks problem, we used single lock on the interpreter that is Global Interpreter Lock(GIL).
Why was the GIL chosen as the solution :
Python supports C language in the backend and all the related libraries that python have are mostly written in C and C++. Due to GIL, Python provides a better way to deal with thread-safe memory management. Global Interpreter Lock is easy to implement in python as it only needs to provide a single lock to a thread for processing in python. The GIL is simple to implement and was easily added to Python. It provides a performance increase to single-threaded programs as only one lock needs to be managed.
Impact on multi-threaded Python programs :
When a user writes Python programs or any computer programs then there’s a difference between those that are CPU-bound in their performance and those that are I/O-bound. CPU push the program to its limits by performing many operations simultaneously whereas I/O program had to spend time waiting for Input/Output. For Example
Code 1: CPU bound program that perform simple countdown
Python3
# Python program showing
# CPU bound program
import time
from threading import Thread
COUNT = 50000000
def countdown(n):
while n>0:
n -= 1
start = time.time()
countdown(COUNT)
end = time.time()
print('Time taken in seconds -', end - start)
Output:
Time taken in seconds - 2.5236213207244873
Code 2: Two threads running parallel
Python3
# Python program showing
# two threads running parallel
import time
from threading import Thread
COUNT = 50000000
def countdown(n):
while n>0:
n -= 1
t1 = Thread(target = countdown, args =(COUNT//2, ))
t2 = Thread(target = countdown, args =(COUNT//2, ))
start = time.time()
t1.start()
t2.start()
t1.join()
t2.join()
end = time.time()
print('Time taken in seconds -', end - start)
Output:
Time taken in seconds - 2.183610439300537
As you can see, In the above code two code where CPU bound process and multi-threaded process have the same performance because in CPU bound program because GIL restricts CPU to only work with a single thread. The impact of CPU bound thread and multi-threading will be the same in python.
Why hasn’t the GIL been removed yet :
GIL is not improved as of now because python 2 having GIL implementation and if we change this in python 3 then it will create a problem for us. So instead of removing GIL, we improve the concept of GIL. It's one of the reasons to not remove the GIL at yet is python heavily depends on C in the backend and C extension heavily depends on the implementation methods of GIL. Although there are many more methods to solve the problems that GIL solve most of them are difficult to implement and can slow down the system.
How to deal with Python’s GIL :
Most of the time we use the multiprocessing to prevent the program from GIL. In this implementation, python provide a different interpreter to each process to run so in this case the single thread is provided to each process in multi-processing.
Python3
# Python program showing
# multiprocessing
import multiprocessing
import time
COUNT = 50000000
def countdown(n):
while n>0:
n -= 1
if __name__ == "__main__":
# creating processes
start = time.time()
p1 = multiprocessing.Process(target = countdown, args =(COUNT//2, ))
p2 = multiprocessing.Process(target = countdown, args =(COUNT//2, ))
# starting process 1
p1.start()
# starting process 2
p2.start()
# wait until process 1 is finished
p1.join()
# wait until process 2 is finished
p2.join()
end = time.time()
print('Time taken in seconds -', end - start)
Output:
Time taken in seconds - 2.5148496627807617
As you can see that there is no difference between the time taken by the multi-threaded system and the multi-processing system. This is because a multi-processing system has their own problems to solve. So this will not solve the problem but yes it provides the solution that GIL allows to be performed by python.
Similar Reads
What is Python Interpreter
Well if you are new to Python you have come across the word interpreters but before knowing about Python interpreters, we need to what is an interpreter, what it will do, the advantages and disadvantages of the interpreter, and so on. Well in this handy article, we will cover the Importance of Inter
4 min read
Why Python is Called Interpreted Language
Python is frequently categorized as an interpreted language, but What does that suggest exactly? To apprehend why Python is called an interpreted language, it's essential to discover the concepts of interpretation and compilation, in addition to the execution model of Python code. Python is called
6 min read
What is the common header format of Python files?
When writing Python scripts, it's important to maintain a clean and well-documented codebase. One of the key practices for achieving this is adding a header to each Python file. The header provides essential information about the script, such as its functionality, author and dependencies, which can
3 min read
How does the Python Interpreter check thread duration?
In Python, threads are a means of achieving concurrent execution. The Python interpreter employs a mechanism to manage and monitor the duration and execution of threads. Understanding how the Python interpreter handles thread duration is essential for developing efficient and responsive multithreade
3 min read
What is the use of Python -m flag?
Python, a versatile and widely used programming language, provides a plethora of features and command-line options to facilitate various tasks. One such option that might pique your interest is the -m switch. In this article, we will explore what Python -m is and how it can be used, accompanied by f
2 min read
What is the send Function in Python Generators
Python generators are a powerful feature that allows for efficient iteration over potentially large datasets without the need to load everything into memory at once. A generator is a special type of iterable that uses the yield keyword to produce a sequence of values, one at a time. In addition to t
4 min read
Python | Compiled or Interpreted ?
Please note that Python language standard does not specify whether the code is to be compiled or interpreted or both. It depends upon the implementation or distribution of a Python language. The most common implementations of Python like CPython do both compilation and interpretation. The compilatio
2 min read
Global and Local Variables in Python
Python Global variables are those which are not defined inside any function and have a global scope whereas Python local variables are those which are defined inside a function and their scope is limited to that function only. In other words, we can say that local variables are accessible only insid
7 min read
Thread-based parallelism in Python
A multi-threaded program consists of sub-programs each of which is handled separately by different threads. Multi-threading allows for parallelism in program execution. All the active threads run concurrently, sharing the CPU resources effectively and thereby, making the program execution faster. Mu
4 min read
Best Python Interpreters: Choose the Best in 2024
Python, known for its versatility and ease of use, offers developers a range of interpreters to suit various needs and preferences. Whether you're aiming for performance, compatibility, or integration with specific platforms, understanding the available options is essential. In this guide, we'll exp
3 min read