all Python 3threadingterm
GIL
concepts
The Global Interpreter Lock — the CPython mutex that allows only one thread to execute bytecode at a time. It blocks parallelism for CPU-bound work, but does not get in the way of I/O.
Syntax
# GIL — концепция, не синтаксис
The GIL does not make your code thread-safe: compound operations (check-then-act, counter += 1) still race between threads, so you still need a Lock. Since Python 3.13 there is a free-threaded build (PEP 703) where the GIL can be turned off — experimental in 3.13, officially supported but optional since 3.14 (PEP 779) — but in the regular build it is still there.
Examples
# GIL мешает CPU-задачам: два потока не быстрее одного
from concurrent.futures import ProcessPoolExecutor, ThreadPoolExecutor
import multiprocessing
def cpu_task(n):
return sum(range(n))
if __name__ == '__main__': # обязательно: процессы-помощники заново импортируют модуль
with ThreadPoolExecutor() as ex: # потоки: байт-код исполняет один поток за раз
print(sum(ex.map(cpu_task, [10**6] * 4))) # → 1999998000000
with ProcessPoolExecutor(mp_context=multiprocessing.get_context('spawn')) as ex: # процессы: у каждого свой интерпретатор и свой GIL
print(sum(ex.map(cpu_task, [10**6] * 4))) # → 1999998000000
See also
Open in the full glossarydocs.python.orgReport an inaccuracy