Vol. 8 — № 22
The Python Loop · KiosqueNewsstand
Blog  
Un atelier Python · Édition d'ApprentissageA Python Workshop · Learning Edition

Le GIL : pourquoi deux threads ne calculent pas en parallèle The GIL: why two threads don't compute in parallel

Lancer deux threads pour accélérer un calcul semble une évidence — sur un CPU à plusieurs cœurs, ils devraient se partager le travail. Pourtant, en Python, un seul thread exécute du bytecode à la fois. Le GIL sérialise le calcul ; seule l'attente — un fichier, un réseau, un timer — libère vraiment un cœur pour un autre thread. Spinning up two threads to speed up a computation looks obvious — on a multi-core CPU, they should split the work. Yet in Python, only one thread executes bytecode at a time. The GIL serializes computation; only waiting — a file, a network call, a timer — actually frees a core for another thread.

AudienceAudience
Dev qui a lancé threading.Thread sur une boucle de calcul et se demande pourquoi ça n'est pas allé plus vite Dev who spun up threading.Thread on a compute loop and wonders why it wasn't any faster
Format
Self-paced
ChapitresChapters
5
Date
Juil 2026 Jul 2026
≈ 19 min ●●●○ GILThreadingConcurrence

Chapitre 1 en accès libre — la suite (ch. 2 à 5) est réservée. Chapter 1 free to read — the rest (ch. 2–5) is members-only.

01CadrageFraming3 min

Deux threads sur un CPU à huit cœurs — et pourtant, aucune accélération. Le calcul reste sérialisé.Two threads on an eight-core CPU — and yet no speedup at all. The computation stays serialized.

Le générateur suspendu du n°21 mettait sa propre exécution en pause, à un point précis, pour rendre la main. Un thread, lui, semble tourner en même temps que les autres — c'est même la promesse du multithreading : répartir le travail sur plusieurs cœurs pour aller plus vite. Cette intuition est fausse en Python, et ce numéro explique pourquoi : le Global Interpreter Lock, le GIL, garantit qu'un seul thread exécute du bytecode Python à un instant donné, quel que soit le nombre de cœurs disponibles. Diviser un calcul en deux threads ne le fait donc pas tourner deux fois plus vite — au mieux, ça ne change rien ; au pire, la coordination entre threads ralentit l'ensemble.Issue №21's suspended generator paused its own execution at a precise point to hand back control. A thread, by contrast, looks like it runs at the same time as the others — that's even the promise of multithreading: spread the work across multiple cores to go faster. That intuition is wrong in Python, and this issue explains why: the Global Interpreter Lock, the GIL, guarantees that only one thread executes Python bytecode at any given instant, no matter how many cores are available. Splitting a computation across two threads therefore doesn't run it twice as fast — at best, nothing changes; at worst, the coordination between threads slows the whole thing down.

Deux threads sur un calcul pur : la durée ne se divise pasTwo threads on a pure computation: the duration doesn't split
import threading, time

def compter(n):
    while n > 0:
        n -= 1

debut = time.perf_counter()
compter(50_000_000)
print("séquentiel :", time.perf_counter() - debut)

debut = time.perf_counter()
t1 = threading.Thread(target=compter, args=(25_000_000,))
t2 = threading.Thread(target=compter, args=(25_000_000,))
t1.start(); t2.start()
t1.join(); t2.join()
print("deux threads :", time.perf_counter() - debut)

# sur un CPU à 8 cœurs, on attend une division par ~2 -- il n'y en a
# quasiment aucune. Parfois, c'est même PLUS LENT que la version
# séquentielle.
Le réflexe du numéroThis issue's reflex

Avant de lancer un threading.Thread, demande : ce travail attend-il quelque chose (I/O), ou calcule-t-il (CPU) ? Le GIL ne pénalise que le second cas.Before spinning up a threading.Thread, ask: is this work waiting on something (I/O), or computing (CPU)? The GIL only penalizes the latter.

🔒

La suite est réservée The rest is members-only

Le premier numéro est libre. Débloque tout The Python Loop — tous les volumes, à vie — pour 5 €, paiement unique. The first issue is free. Unlock all of The Python Loop — every volume, forever — for €5, one-time.

Retour au kiosqueBack to newsstand