Sunday 20 December 2015

Python: Global interpreter lock

Global interpreter lock (GIL) is a mechanism used by interpreters to serialize the access to their internal code. Global interpreter lock imposes performance restriction on threads. For example, in multi core systems, you can't utilize all the cpus efficiently. CPython implements Global interpreter lock. Jython and IronPython are not implemented GIL.

What are the reasons to implement GIL?
a.   Single threaded environment works faster, since no need to acquire and release locks
b.   C libraries can be easily integrated.
c.    You can implement interpreters easily.

Drawbacks of using GIL
a.   Multi threaded programs failed to take advantage of multi core systems. It is because, suppose two threads are trying to execute same 100 lines of byte code, thread1 gets lock and process the code and release the lock. Second thread will be notified, get the lock, process the byte code and release the lock. Locking, unlocking, notifying takes some time. So Two threads calling a function may take more time as a single thread calling the function twice.

Python multiprocessing library used to overcome the drawback of GIL. It supports spawning processes using an API similar to the threading module. By using multiprocessing module, programmers fully leverage multiple processors on a given machine.


Previous                                                 Next                                                 Home

No comments:

Post a Comment