4
Python's Global Interpreter Lock (GIL)
Python
GIL

Multi-threaded programming has always been a weak point for the CPython implementation of the Python language. In this post , whenever i refer to Python i mean the CPython (python.org) implementation, that is the most used implementation of Python.

Threads are useful in programs which use parallel processing to perform heavy computations. However, Python has surprising behavior when it comes to threading, especially noticeable in case of multicore processors. Python has Global Interpreter Lock, commonly known as GIL, similar to a mutex in behavior.

This essentially means is a process can run only one thread at a time. When a thread starts running, it acquires GIL and when it waits for I/O, it releases the GIL, so that other threads of that process can run.

For e.g.,

Let us suppose process P1 has threads t1 and t2. Python threads are native threads, that means they are scheduled by the underlying operating system.

t1 running (acquire GIL) -> t1 waiting for I/O (releases GIL) -> t2 running (acquires GIL, by this time t1 is also ready but GIL is acquired by t2)

Hence, we find that GIL is a major restriction, if we want to write multi threaded python application that does heavy CPU bound operations. Because, real multi threading is not possible, as effectively the process will be utilizing only one CPU at a single time even in multicore CPU.

However, in most applications of Python (web application, sysadmin scripts, etc.), this is not really an issue as these applications are I/O bound. As a Python programmer, you never have to deal with acquiring and releasing GIL unless you are writing C/C++ modules, in that case you should take care of acquiring and releasing GIL.

Author

Notifications

?