Skip to main content

Command Palette

Search for a command to run...

How to Disable GIL in Python 3.13?

Updated
3 min read
How to Disable GIL in Python 3.13?
S

I am a self-taught Python developer who loves to write on Python Programming and quite obsessed with Machine Learning.

The Python organization has released the official version of Python 3.13, which includes many major changes.

One of the major changes we get is making GIL (Global Interpreter Lock) optional in Python 3.13.

In this article, you’ll see how to disable GIL in Python 3.13 to make multi-threaded programs faster.

Disable GIL in Python 3.13

First, download the official Python 3.13 on your local machine. Then proceed to install the Python executable file.

Follow the steps just as you install any Python version, you will see a step in which you are asked to choose the advanced installation options.

Advance free-threaded option

Now select the checkbox appearing at the end, this will install the different executable Python build with the name python3.13t or python3.13t.exe.

Enable free-threaded option

This will allow you to optionally disable or enable GIL in the runtime. Now proceed to install the setup.

After completing the setup for Python 3.13, if you look at the file location of Python 3.13, you will see that you also got a different Python build called python3.13t.

Free-threaded CPython

If you remember that “free-threaded binaries” mode was marked “experimental”, this means that you don’t get GIL disabled by default.

But how do I disable GIL? Python organization provided a different CPython build which is also called “Free-threaded CPython”.

Free-threaded CPython will help you disable GIL. Let’s see how to do it.

Let’s say, you have a Python script and you want it to run with GIL disabled. You can use the following command.

python3.13t main.py

This CPython build comes with GIL disabled by default.

Practical

I have the following Python script (main.py) that runs a multi-threaded task and calculates the time taken to complete the execution.

import sys
import sysconfig
import math
import time
import threading

def compute_factorial(n):
    return math.factorial(n)

# Multi-threaded
def multi_threaded_compute(n):
    threads = []
    # Create 5 threads
    for num in n:
        thread = threading.Thread(target=compute_factorial, args=(num,))
        threads.append(thread)
        thread.start()

    # Wait for all threads to complete
    for thread in threads:
        thread.join()

    print("Factorial Computed.")

def main():
    # Checking Version
    print(f"Python version: {sys.version}")

    # GIL Status
    status = sysconfig.get_config_var("Py_GIL_DISABLED")
    if status is None:
        print("GIL cannot be disabled")
    if status == 0:
        print("GIL is active")
    if status == 1:
        print("GIL is disabled")

    numlist = [100000, 200000, 300000, 400000, 500000]

    # Multi-threaded Execution
    start = time.time()
    multi_threaded_compute(numlist)
    end = time.time() - start
    print(f"Time taken : {end:.2f} seconds")

if __name__ == "__main__":
    main()

Now, we’ll run this script with and without free-threaded CPython.

With GIL enable (without free-threaded CPython)

python main.py

Python running with GIL enabled

With GIL disable (with free-threaded CPython)

python3.13t main.py

Python running with GIL disabled


That’s all for now.

Keep Coding✌✌

E
Erik Berg7mo ago

I still can’t believe what happened to me! For years, I struggled to win anything from the lottery until I came across Dr. Benjamin, a powerful spell caster. At first, I was skeptical, but after speaking with him, I decided to give it a try. Dr. Benjamin prepared a special spell and gave me the exact lottery numbers to play. I followed his instructions carefully, and to my greatest surprise, I hit the Powerball jackpot worth $340,000,000.00! My life has completely changed overnight — from financial struggles to total freedom. I can now take care of my family, help people in need, and live the kind of life I always dreamed of. Dr. Benjamin is truly a blessing, and I’ll forever be grateful for his amazing help. I promise not to ever stop telling people about the goods works you've done in my life.. If you need his help reach him today via email drbenjaminlottospell711@gmail.com

More from this blog

P

Python Programming Tutorials and Articles - GeekPython

107 posts

Welcome to Team Geek, your ultimate destination for an array of programming articles crafted by our talented developers.