Limitations of multithreading in python
In Python GIL stands for global interpreter lock.
we can understand it as a lock on python byte code, preventing multiple threads to execute the same byte code at same time. hence, It make sure that only one thread can execute the byte code at a time.
How it works?
When a python interpreter starts executing the program, It creates a GIL, when a thread start executing a
python byte code, it acquires GIL and process the byte code, until it is released other threads must wait before they can acquire it and execute the byte code.
Why is GIL required?
The GIL is required due to CPython’s memory management mechanism. CPython (the reference implementation of Python) manages memory through a mechanism called reference counting. The GIL simplifies the implementation of this mechanism by ensuring that reference count manipulations are atomic, which simplifies memory management. However, this means that only one thread can execute Python bytecode at a time, which can limit the performance of multi-threaded Python programs, especially on multi-core systems.
Impact on performance
GIL has significant impact on performance of multithreaded python programs, particularly on those tasks which are cpu intensive because only one thread can execute python byte code while other threads keep waiting until the GIL is released.
Workarounds
Use multiprocessing instead of multithreading, multiprocessing executes in many processes running in parallel each process having its own python interpreter, threads and GIL.
Use asynchronous programming methods like asyncio , which allows you to write concurrent code, that cooperatively multitasks without using multiple threads.