You are currently viewing GIL Become Optional in Python 3.13

GIL Become Optional in Python 3.13

GIL or Global Interpreter Lock can be disabled in Python version 3.13. This is currently experimental.

What is GIL? It is a mechanism used by the CPython interpreter to ensure that only one thread executes the Python bytecode at a time.

An Experimental Feature

Python 3.13 brings major new features compared to Python 3.12 and one of them is free-threaded mode, which disables the Global Interpreter Lock, allowing threads to run more concurrently.

This is an experimental feature and if you want to try it, you can download the beta version of Python 3.13 from here.

At the time of installation, check the option “free threaded binaries(experimental)” to get the feature in Python.

Making GIL Optional in Python 3.13

The GIL will be disabled when you configure the Python with the --disable-gil option which is nothing but a build configuration (free threading build) at the time of installation.

This will allow optionally enabling and disabling GIL using the environment variable PYTHON_GIL which can be set to 1 and 0 respectively.

It will also provide a command-line option -X gil which can also be set to 0 (disable) and 1 (enable).

We can also check if the current interpreter is configured with the free threading build (--disable-gil) by using the following code.

If we run this, we’ll get either 0 which means GIL is enabled, or 1 which means GIL is disabled.

With this, we’ll also get a function that can be used to check if GIL is disabled in the running process.

GIL Vs No GIL

Let’s see how the performance of multi-threaded programs will be affected when GIL is enabled and disabled.

We have a simple Python program (gil.py) that computes the factorial of numbers and compares the execution time taken by single-threaded, multi-thread, and multi-process tasks. We’ll run this Python program first with GIL and then without GIL.

Running gil.py with GIL

We have Python v3.12 in which there is no option to check the GIL status, so we got “GIL cannot be disabled”.

The difference is not that much between the single-threaded and multi-threaded but we can see a pretty decent difference in the case of multi-process task.

Running gil.py without GIL

This time we have a third beta version of Python 3.13 configured with free threading build and as we can see the GIL is disabled.

But the most important part is we can see a massive difference in the performance of multi-threaded task and on the other side, some degradation can be seen in the performance of multi-process and single-threaded task.


πŸ†Other articles you might be interested in if you liked this one

βœ…Create multi-threaded Python programs using a threading module.

βœ…Template inheritance in Flask.

βœ…Difference between exec and eval in Python.

βœ…Create temporary files and directories using tempfile module in Python.

βœ…Upload and display images on the frontend using Flask.

βœ…How does the learning rate affect the ML and DL models?


That’s all for now

Keep Coding✌✌