L6·Expert
🧵Threads, Processes & the GIL
Concurrency vs parallelism in CPython.
threadingmultiprocessingGIL
§ 1The GIL
CPython's Global Interpreter Lock allows only one thread to execute Python bytecode at a time. Threads are still great for I/O.
§ 2Threads
threading.Thread(target=fn).start() — cheap, share memory, fine for I/O-bound.
§ 3Processes
multiprocessing bypasses the GIL by spawning separate interpreters. Best for CPU-bound.
Example
1from concurrent.futures import ThreadPoolExecutor2import time34def slow(i):5 time.sleep(0.2)6 return i * i78t = time.perf_counter()9with ThreadPoolExecutor(max_workers=5) as pool:10 print(list(pool.map(slow, range(5))))11print(f"{time.perf_counter()-t:.2f}s") # ~0.2s not 1.0sTry it live · Python runs in your browser
Loading playground…
Checkpoint quiz
Question 1 / 2
For CPU-bound Python work, prefer: