Python Hard Parts (intermediate level)

Ayush Singh
1 min readJun 8, 2022

GIL — The Global Interpreter Lock

In CPython, due to the Global Interpreter Lock, only 1 thread can execute a python code at once ( certain performance-oriented libraries might overcome limitation)

This means that no matter how many threads you create in a current process(core), the combined cpu usage of all threads will never cross 100% cpu usage i.e it will treat your cpu as a single core cpu. This means that : a Python process cannot run threads in parallel but it can run them concurrently through context switching during I/O bound operations.

Note : This scenario is not valid for all programming languages. If you want to achieve parallelism use multiprocessing

When to use what ?

In Python, during cpu intensive tasks, multiple threads will not create much impact. But in other jobs like network reading, file reading, I/O, connecting to webserver and getting data back. In those scenarios, cpu usage need not be 100% by each of thread as it has to wait for network input, disk input and those things are slower when compared to cpu, that means cpu will be free and we can make use of multithreads.

--

--