DEV Community

Cover image for GIL in Python
Aniket Singh for eduAlgo

Posted on

GIL in Python

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')
    time.sleep(1)
    print ('Done sleeping')

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

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

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

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....')
    time.sleep(1)
    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
t1.start()
t2.start()

t1.join()
t2.join()

finish = time.perf_counter()

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

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.

Oldest comments (3)

Collapse
 
ksaaskil profile image
Info Comment hidden by post author - thread only accessible via permalink
Kimmo Sääskilahti • Edited

Thanks for the article!

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.

It's maybe worth stressing here that multi-threading can make execution a lot faster. It requires that threads release the GIL when e.g. waiting on I/O. Well-written libraries like NumPy release the GIL whenever possible, allowing multiple computations to proceed in parallel.

Threading runs on a single processor and therefore only runs one at a time.

Single Python process can use multiple cores at the same time in parallel. Try starting e.g. two threads each making a numerical computation with NumPy and you may see the process CPU usage going up to 200 %.

Collapse
 
aniketsingh98571 profile image
Aniket Singh

yes, it can be taken into consideration although writing on such advanced topics was quite challenging for me, thanks for your feedback noted it.

Collapse
 
aditi_jha profile image
Aditi Jha

Awesome explanation!👏👏

Some comments have been hidden by the post's author - find out more