Home Backend Development Python Tutorial Python The Gateway to High-Performance Multithreading Without GIL

Python The Gateway to High-Performance Multithreading Without GIL

Jan 06, 2025 pm 04:58 PM

Introduction

Python has long been known for its ease of use and versatility, but one topic that has sparked much discussion in the Python community is the Global Interpreter Lock (GIL). The GIL has been both a safeguard and a bottleneck for Python's concurrency model, especially for CPU-bound tasks that could otherwise take advantage of multiple CPU cores. With the release of Python 3.13, however, Python developers have a groundbreaking new option: the ability to disable the GIL. This blog will explore what the GIL is, why it has been an obstacle for performance in multithreading, and how to detect and disable the GIL in Python 3.13 to unlock true multithreading performance.

What is Global Interpreter Lock (GIL)

The Global Interpreter Lock (GIL) is a mutex that protects access to Python objects, preventing multiple native threads from executing Python bytecode at once. This ensures thread safety for Python programs but at the cost of concurrent execution. The GIL makes Python threads more efficient for I/O-bound tasks but limits their performance for CPU-bound tasks.

Why the GIL is a Bottleneck for Multithreading

Python's GIL allows only one thread to execute simultaneously, even in multithreaded programs. While this is fine for I/O-bound tasks where the program is waiting for input/output operations, it severely limits performance for CPU-bound tasks like number crunching, data analysis, or image processing.

Python 3.13: Unlocking Multithreading with GIL Disabled

With Python 3.13, developers have the option to disable the GIL during the Python build process. However, disabling the GIL is not available in pre-built Python distributions. Instead, you must compile Python 3.13 from source with the --disable-gil option.

This new option opens the door for true parallelism in CPU-bound multithreaded tasks, allowing threads to execute in parallel across multiple cores.

Prerequisites for Using Python 3.13 Without GIL

  • Python 3.13 Source Code: Disabling the GIL is not available in standard pre-built binaries. You must build Python 3.13 from the source with the --disable-gil flag.
  • Multi-Core CPU: You need a multi-core CPU to benefit from true multithreading, as threads will now run in parallel across multiple cores.

Compiling Python 3.13 with GIL Disabled

To disable the GIL using the -X gil=0 flag, you need to compile Python from source with the --disable-gil flag enabled. Here's how to do that

Step-by-Step

  • Download Python 3.13 Source Code You first need to download the Python 3.13 source code tarball from the official Python website. This is because the pre-built binaries (like the ones you download directly from python.org) are not compiled with support for disabling the GIL. You can download it using the web browser or using wget or even using curl in your terminal
wget https://www.python.org/ftp/python/3.13.0/Python-3.13.0.tgz
Copy after login
  • Extract the Source:
tar -xf Python-3.13.0.tgz
cd Python-3.13.0
Copy after login
  • Configure the build with --disable-gil You need to configure Python with --disable-gil to support the option of disabling the GIL.
./configure --disable-gil
Copy after login
  • Compile and install Python:
make
sudo make altinstall 
Copy after login
  • If altinstall step fails, then Re-run the configure Command with --prefix
./configure --disable-gil --prefix=$HOME/python3.13
Copy after login
  • Run make altinstall in the Specified Directory Then, run the make altinstall command
make altinstall
Copy after login

How to Detect GIL in Python 3.13

In Python 3.13, you can check whether the GIL is enabled or disabled using the sys._is_gil_enabled() function.

import sys

def check_gil_status():
    if sys.version_info >= (3, 13):
        status = sys._is_gil_enabled()
        if status:
            print("GIL is currently enabled.")
        else:
            print("GIL is currently disabled.")
    else:
        print("Python version does not support GIL status detection.")

check_gil_status()
Copy after login

Hands-On: Python Multithreading with GIL vs GIL-Free

The following Python code was developed to assess the performance gains when disabling the GIL in Python 3.13. The script executes eight threads concurrently, each tasked with calculating the prime factors of large numbers. By leveraging true parallelism, the code highlights the enhanced performance achieved without the GIL.

#!/usr/bin/env python3
import sys
import sysconfig
import time
from threading import Thread
from multiprocessing import Process


# Decorator to measure execution time of functions
def calculate_execution_time(func):
    def wrapper(*args, **kwargs):
        start_time = time.perf_counter()
        result = func(*args, **kwargs)
        end_time = time.perf_counter()
        execution_time = end_time - start_time
        print(f"{func.__name__} took {execution_time:.4f} seconds.")
        return result

    return wrapper


# Compute-intensive task: Iterative Fibonacci calculation
def compute_fibonacci(n):
    """Compute Fibonacci number for a given n iteratively."""
    a, b = 0, 1
    for _ in range(n):
        a, b = b, a + b
    return a


# Single-threaded task execution
@calculate_execution_time
def run_single_threaded(nums):
    for num in nums:
        compute_fibonacci(num)


# Multi-threaded task execution
@calculate_execution_time
def run_multi_threaded(nums):
    threads = [Thread(target=compute_fibonacci, args=(num,)) for num in nums]
    for thread in threads:
        thread.start()
    for thread in threads:
        thread.join()


# Multi-processing task execution
@calculate_execution_time
def run_multi_processing(nums):
    processes = [Process(target=compute_fibonacci, args=(num,)) for num in nums]
    for process in processes:
        process.start()
    for process in processes:
        process.join()


# Main execution function
def main():
    # Check Python version and GIL status for Python 3.13+
    print(f"Python Version: {sys.version}")

    py_version = float(".".join(sys.version.split()[0].split(".")[:2]))
    status = sysconfig.get_config_var("Py_GIL_DISABLED")

    if py_version >= 3.13:
        status = sys._is_gil_enabled()

    if status is None:
        print("GIL cannot be disabled for Python <= 3.12")
    elif status == 0:
        print("GIL is currently disabled")
    elif status == 1:
        print("GIL is currently active")

    # Run tasks on the same input size for comparison
    nums = [300000] * 8

    print("\nRunning Single-Threaded Task:")
    run_single_threaded(nums)

    print("\nRunning Multi-Threaded Task:")
    run_multi_threaded(nums)

    print("\nRunning Multi-Processing Task:")
    run_multi_processing(nums)


if __name__ == "__main__":
    main()

Copy after login

Analysis:

## Python 3.13 with GIL Disabled
Python Version: 3.13.0 experimental free-threading build (main, Oct 14 2024, 17:09:28) [Clang 14.0.0 (clang-1400.0.29.202)]
GIL is currently disabled

Running Single-Threaded Task:
run_single_threaded took 8.6587 seconds.

Running Multi-Threaded Task:
run_multi_threaded took 1.3885 seconds.

Running Multi-Processing Task:
run_multi_processing took 1.5953 seconds.

## Python 3.13 with GIL Enabled
Python Version: 3.13.0 experimental free-threading build (main, Oct 14 2024, 17:09:28) [Clang 14.0.0 (clang-1400.0.29.202)]
GIL is currently active

Running Single-Threaded Task:
run_single_threaded took 8.7108 seconds.

Running Multi-Threaded Task:
run_multi_threaded took 8.6645 seconds.

Running Multi-Processing Task:
run_multi_processing took 1.4530 seconds.

## Python 3.12 
Python Version: 3.12.6 (main, Sep  7 2024, 19:30:10) [Clang 14.0.0 (clang-1400.0.29.202)]
GIL cannot be disabled for Python <= 3.12

Running Single-Threaded Task:
run_single_threaded took 8.7004 seconds.

Running Multi-Threaded Task:
run_multi_threaded took 8.6297 seconds.

Running Multi-Processing Task:
run_multi_processing took 1.4876 seconds.
Copy after login

Multi-threaded performance: The real benefit of disabling the GIL is evident in the multi-threaded scenario:

With GIL disabled (3.13), the execution time is 1.5703 seconds.
With GIL enabled (3.13), the execution time is 8.5901 seconds.
Result: Disabling the GIL resulted in a performance improvement of approximately 81.7% for multi-threaded tasks.

Python The Gateway to High-Performance Multithreading Without GIL

Python The Gateway to High-Performance Multithreading Without GIL

The chart clearly demonstrates that disabling the GIL in Python 3.13 leads to a substantial performance boost for multi-threaded CPU-bound tasks, allowing Python to efficiently utilize multiple CPU cores in parallel. While single-threaded and multi-processing performance remains largely unaffected, multi-threaded performance shows a significant improvement, making Python 3.13 a game-changer for CPU-intensive applications that rely on multi-threading.

However, Python versions prior to 3.13 do not support disabling the GIL, which explains why their multi-threaded performance remains similar to that of Python 3.13 with the GIL enabled. This limitation in earlier versions continues to restrict Python's ability to fully exploit multi-threading for CPU-bound tasks.

Key Considerations Before Disabling the GIL

Disabling the Global Interpreter Lock (GIL) in Python 3.13 can unlock significant performance improvements in multi-threaded CPU-bound tasks. However, there are several important factors to consider before doing so:

  • Thread Safety: Without the GIL, you must manually handle thread safety using locks or other synchronization mechanisms to prevent race conditions in your code.

  • Potential Performance Degradation: Fine-grained locking can introduce contention, which may degrade performance in single-threaded or I/O-bound tasks that previously benefited from the GIL.

  • Compatibility with Third-Party Libraries: Many C extensions and libraries assume the presence of the GIL for thread safety. Disabling the GIL might require updates to these libraries to ensure they work correctly in a multithreaded environment.

  • Complex Memory Management: Disabling the GIL introduces more complexity in memory management, requiring thread-safe memory handling which can increase the risk of bugs and errors.

  • I/O-bound Tasks: Disabling the GIL provides limited benefits for I/O-bound tasks, where non-blocking I/O mechanisms like asyncio might be more effective.

  • Difficulty in Debugging: Without the GIL, debugging multithreaded applications can become more challenging due to the increased likelihood of race conditions and deadlocks.

  • Higher Memory Usage: Using locks and managing thread states without the GIL can increase memory consumption, particularly in multi-threaded applications.

  • Embedded Systems: Disabling the GIL might complicate Python's integration with multi-threaded environments in embedded systems, requiring more effort for effective integration.

  • Lock Contention: In some cases, disabling the GIL can lead to lock contention between threads, which might reduce the expected performance improvements.

GitHub Repository

You can find the complete source code for the examples in this blog on my GitHub:

Python GIL Performance Analysis

Disclaimer:

This is a personal blog. The views and opinions expressed here are only those of the author and do not represent those of any organization or any individual with whom the author may be associated, professionally or personally.

The above is the detailed content of Python The Gateway to High-Performance Multithreading Without GIL. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to solve the permissions problem encountered when viewing Python version in Linux terminal? How to solve the permissions problem encountered when viewing Python version in Linux terminal? Apr 01, 2025 pm 05:09 PM

Solution to permission issues when viewing Python version in Linux terminal When you try to view Python version in Linux terminal, enter python...

How to avoid being detected by the browser when using Fiddler Everywhere for man-in-the-middle reading? How to avoid being detected by the browser when using Fiddler Everywhere for man-in-the-middle reading? Apr 02, 2025 am 07:15 AM

How to avoid being detected when using FiddlerEverywhere for man-in-the-middle readings When you use FiddlerEverywhere...

How to teach computer novice programming basics in project and problem-driven methods within 10 hours? How to teach computer novice programming basics in project and problem-driven methods within 10 hours? Apr 02, 2025 am 07:18 AM

How to teach computer novice programming basics within 10 hours? If you only have 10 hours to teach computer novice some programming knowledge, what would you choose to teach...

How to efficiently copy the entire column of one DataFrame into another DataFrame with different structures in Python? How to efficiently copy the entire column of one DataFrame into another DataFrame with different structures in Python? Apr 01, 2025 pm 11:15 PM

When using Python's pandas library, how to copy whole columns between two DataFrames with different structures is a common problem. Suppose we have two Dats...

How does Uvicorn continuously listen for HTTP requests without serving_forever()? How does Uvicorn continuously listen for HTTP requests without serving_forever()? Apr 01, 2025 pm 10:51 PM

How does Uvicorn continuously listen for HTTP requests? Uvicorn is a lightweight web server based on ASGI. One of its core functions is to listen for HTTP requests and proceed...

How to solve permission issues when using python --version command in Linux terminal? How to solve permission issues when using python --version command in Linux terminal? Apr 02, 2025 am 06:36 AM

Using python in Linux terminal...

How to get news data bypassing Investing.com's anti-crawler mechanism? How to get news data bypassing Investing.com's anti-crawler mechanism? Apr 02, 2025 am 07:03 AM

Understanding the anti-crawling strategy of Investing.com Many people often try to crawl news data from Investing.com (https://cn.investing.com/news/latest-news)...

See all articles