Python advanced #001 GIL (2020)


 

1. What is GIL?

The GIL (global interpreter lock) is used by CPython interpreter which written in C programming language. There are other python interpreters that are written in other languages, such as JPython written in Java.

CPython uses this GIL lock to make sure that only one thread executes python bytecode at a time. The other thread must wait for its release.

Using this GIL, CPython ensures that when a thread is executing python bytecode, any python objects will not be accessed by another thread. It provides a safe threading way to achieve multitasking.

Its downside is more obvious in modern computers which always feature multi-core cpus. Because only one thread could execute code at a time, CPython will only occupy one cpu core at a time leaving other cpu cores idle. 

1.1 Why Guido doesn't remove it

Actually, Guido (creator of Python) did that several years ago. But eventually, he quit and make a statement that removing the GIL is not an easy task; by removing GIL could suffer the performance of the python programs in a single-processor case and overcoming this performance issue is believed to make this implementation much more complicated and costly to maintain.

1.2 Does multi-threads program run faster than a single thread one because of GIL?

The answer is "yes" or "no".

When you are doing IO tasks using multi-threads, your program definately run faster than a single-tread program.

IO tasks include networking, read/write files etc. For instance:

client_socket.recv(1024)

When we want to receive data from a client socket (refer to networking topics in this blog), the program will block here and wait for incoming data. After 5 minutes, data comes and the program no longer block and continue to execute the next code and then handle the next incoming data.

If there are three requests at the same time, our single-thread program could only handle one at a time which will take 5 minutes. So after all, it will cost 15 minutes to handle all those tasks.

However, if we use multi-threads, when the first task is waiting for the incoming data in one thread, our GIL lock will be released and other threads could execute its code and start to receive data. So it only takes 5 minutes to receive all those three requests.





Comments

Popular posts from this blog

How to write a slide puzzle game with Python and Pygame (2020 tutorial)

How to create a memory puzzle game with Python and Pygame (#005)

Introduction to multitasking with Python #001 multithreading (2020 tutorial)