Illustration of Python Threading, Processing and GIL by Diagrams

Don’t run away if you are a learner of Python, because this article is meant to use the easiest way to explain to you what is GIL. Of course, it has to be started by explaining what are threads and processes. Don’t worry, I’ll try my best to make it easy for everyone, though it sacrifices some accuracy of the definition.

Now we should begin.

Image by Steen Jepsen from Pixabay

Some Concepts

Multi-threading is one of the most common programming techniques, which also exists in Python.

It allows us to run multiple operations simultaneously. Usually, multi-threading may create extra efficiency in CPU usage. Also, most of the I/O tasks can benefit from concurrently running threads.

Please don’t be confused about the concepts “process” and “thread”. A process will have certain memory allocated and is completely isolated from other processes in an operating system. Therefore, one program crushed in our OS usually will not impact the others.

Relationships between Processes and Threads

A process may have multiple threads running under it, sharing lots of the same resources like memory. Therefore, one thread crushed will cause the entire process to crush. Because the threads share memory with each other, it may also create troubles in the process. I’ll demonstrate later.

Code Example

Now, let’s see how to write Python code with the multi-threading technique.

Firstly, let’s import the threading module that is built-in to Python.

import threading

To be able to test the multi-threading, let’s define a function that is simple enough but will take some time.

def compute()…

Leave a Reply