[Python] Multithreading issue with GIL for CPU-bound task in Python

Discussão em 'Python' iniciado por Stack, Maio 28, 2025.

  1. Stack

    Stack Membro Participativo

    I'm learning about multithreading in Python, and am experimenting to see how GIL(Global Interpretor Lock) works.

    Given the following python code:

    def cpu_bound():
    i = 0
    for _ in range(10000000):
    i += 1


    With the above code, my computer runs in 0.45 seconds. In joblib.Parallel(n_jobs=8, prefer="threads"), I create 12 threads to execute this code. As far as I know, in python there is GIL so actually only 1 python thread is running at a time. If so, to run all 12 threads will take at least 0.45 * 12 = 5.4 seconds (not including the time for context-switching, creating threadpool), but in reality it only takes 3.25 seconds to run 12 threads. Please help me explain the reason.

    Continue reading...

Compartilhe esta Página