CPU-bound thread under GIL (ARCH2042)
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.
Incorrect
Section titled “Incorrect”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 GILCorrect
Section titled “Correct”from concurrent.futures import ProcessPoolExecutor
def crunch(chunks): with ProcessPoolExecutor() as pool: return list(pool.map(cpu_heavy, chunks)) # real parallelismUse ProcessPoolExecutor or multiprocessing instead of threads for CPU-bound work.