Python has the several implementations like CPython, Jython, IronPython etc.The most popular among all is the CPython.It was not written keeping the multi-core processor in mind.The memory management in CPython is not thread-safe.The GIL(Global Interpreter Lock) is implemented which is not a simple mutex lock.GIL prevents the multiple threads to execute at once.
The GIL is the lock on the interpreter and it is implemented on the basis of ticks and check. Ticks are loose interpreter instructions and after every 100 consecutive tick a check is made to release the lock and give chance to other thread to execute on CPU.
On single core CPU the thread works as expected and after every check an another thread waiting for GIL acquires it and get executed.
It causes the penalty in case of multicore CPU system. GIL on multi-core machine does not work as expected, rather it degrades the performance.Lets take 2 threads and both are executing in the two different core. The first thread has acquired the GIL and is executing on the first core, as it releases the lock, by the time the second thread receives the signal, the first thread reacquires it and the second one keeps on waiting for long.
The ambiguity among the Python Interpreter to release or acquire the threads and the OS scheduling of threads also led to the situation that the highest priority threads sometimes did not get the lock and os continues to run the low priority thread, which has a bad impact on the overall performance.
To increase the CPU bound process task performance the time.sleep can be implemented to mock it like the I/O operation, which can help to switch the thread and gives the chances to other CPU bound threads to execute.
Benefits Of GIL:
- Great option for I/O Bound Application. If threads are spending most of the time in I/O then there will be less fighting within the thread to acquire and release the GIL.
- Threads in Python are very light weight, they have a very low memory footprints.