DEV Community

fabriciomsdev
fabriciomsdev

Posted on

What is Python GIL? How it works?

Things you should know before reading this article:

  • What is Parallelism?
  • What is Concurrency?
  • What is a Deadlock?
  • What is Race Concurrency?
  • What is a Process?
  • What is a Thread?

Introduction

The Global Interpreter Lock, is a lock that protects access to Python objects and carefully controls thread execution, preventing race concurrency in data access and modification, ensuring that only one thread can execute Python code at a time.

Without the GIL, Python’s memory management can be not thread-safe, it could lead to inconsistencies and crashes. (Deadlocks)

2 - How it works?

It's very simple, Thread will hold the GIL when it is running, and after running the Thread will release the GIL. The next threads must request access to the GIL in order to execute Opcodes (low-level operations). I draw one example of GIL behavior below:

  • Moment 1:

Image description

  • Moment 2:

Image description

  • Moment 3:

Image description

It means that Python developers can utilize async code, and multi-threaded code and never have to worry about acquiring locks on any variables in the process running or having processes crash from deadlocks.

3 - Pros of using GIL:

  • It simplifies the implementation of CPython's memory management, avoiding race conditions.
  • This mechanism ensures that Python's core data structures, like dictionaries and lists, are thread-safe without requiring complex locking mechanisms.
  • The GIL makes it easier to integrate C extensions with Python and allows the use of CPython, the most common interpreter and compiler used by the community.

4 - Cons of using GIL:

  • The most significant drawback of the GIL is that it prevents Python programs from taking full advantage of multi-core CPUs using multi-threading.
  • In CPU-bound applications, the GIL can become a significant bottleneck, as it prevents true parallel execution of threads
  • As Developer, you may face challenges when trying to optimize multi-threaded Python programs.

5 - How to Deal With GIL Cons?

Instead of using threads, you can use processes to run your algorithms in some cases. For IO/Bound operations Threading and concurrency can allow you to have a better use of your resources, for CPU/Bound operations you can use the multiprocessing library to better resource use.

Top comments (0)