The Developer Quest

The Developer Quest

GIL in Python

GIL in Python

·Feb 7, 2021·

3 min read

Subscribe to my newsletter and never miss my upcoming articles

What is Threading?

Threading in python is used to run multiple threads (tasks, function calls) at the same time. This means that your program will have two things happening at once. Threading is a feature usually provided by the operating system. Each thread has its own memory space.

Python code without thread.

#Program for time estimation of a function without threads.
#import time module
import time

#initializing start variable with time in seconds before the code execution
start = time.perf_counter()

def do_something ():
    print ('Sleeping 1 second')
    print ('Done sleeping')

#If we call the do_something() once it will take 1.01 seconds for execution.    

#But if call the do_something() twice, it will take upto 2.02 seconds for exection.
#The program runs synchronously.

finish = time.perf_counter ();
print ('Finished in' + str(round(finish-start,2)) + ' seconds')

Python code with thread.

#Program for time estimation of a function with threads.
#import module
import time
import threading

start = time.perf_counter()

def do_something ():
    print('Sleeping 1 second....')
    print ('Done sleeping...')

#Assigning thread to each function for parallel execution.
t1 = threading.Thread(target = do_something)
t2 = threading.Thread(target = do_something)

#Telling our OS to execute the threads


finish = time.perf_counter()

#Because of threads process gets completed in 1.02 seconds
print ('Finished in' + str(round(finish-start,2)) + ' seconds')

In threading, the operating system actually knows about each thread and can interrupt it at any time to start running a different thread. Threading runs on a single processor and therefore only runs one at a time.

What is Concurrency?

The dictionary definition of concurrency is simultaneous occurrence. Concurrency is great for IO-bound operations such as for reading a file or communicating over a network. If many threads start executing simultaneously then we can say that it is concurrent.

Process vs Thread

Alt Text

What is Multithreading?

In multithreading, the concept of threads is used. Multithreading is defined as the ability of a processor to execute multiple threads concurrently.

GIL[Global Interpreter Lock]

The Python Global Interpreter Lock or GIL, in simple words, is a mutex (or a lock) that allows only one thread to hold the control of the Python interpreter. This means that only one thread can be in a state of execution at any point in time. The impact of the GIL isn’t visible to developers who execute single-threaded programs. 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. Python has a reference count variable that keeps track of the number of references that point to the object. When this count reaches zero, the memory occupied by the object is released.

[This article has been contributed by Mr Aniket, Thanks Mr. Aniket 🤗]

Share this