Zum Inhalt springen

CPU-bound thread under GIL (ARCH2042)

Dieser Inhalt ist noch nicht in deiner Sprache verfügbar.

Key python.async.cpu-bound-thread-gil · Severity info · Category async

Spawning threads for CPU-bound work in a GIL-constrained runtime like CPython gives no speedup; threads run only one Python bytecode at a time. Use processes for CPU parallelism instead.

import threading
def crunch(chunks):
threads = [threading.Thread(target=cpu_heavy, args=(c,)) for c in chunks]
for t in threads:
t.start() # does not parallelize under the GIL
from concurrent.futures import ProcessPoolExecutor
def crunch(chunks):
with ProcessPoolExecutor() as pool:
return list(pool.map(cpu_heavy, chunks)) # real parallelism

Use ProcessPoolExecutor or multiprocessing instead of threads for CPU-bound work.