The Symptom
Symptom
Root Cause Analysis
Root Cause
Python's Global Interpreter Lock (GIL) prevents multiple native threads from executing Python bytecodes at once. While `threading` works great for I/O-bound tasks (like making network requests), it completely blocks on CPU-bound tasks (like matrix math for image filters). The threads were thrashing violently as they fought for the GIL, adding massive context-switching overhead.
import concurrent.futures
# The fatal flaw: Using threads for CPU-bound work in Python
with concurrent.futures.ThreadPoolExecutor(max_workers=4) as executor:
results = list(executor.map(heavy_cpu_image_filter, image_chunks))
Discussions 0