Python 3.12 GIL: threading won't help, and multiprocessing hit `Can't pickle local object`
We had a nightly job that hashes about 1.2 million records and computes a few rollups. On our Debian 12 box with Python 3.12.4 it took 41 minutes. Someone suggested threads. I should have known better, but I tried it anyway, which is how you learn things.
## The threading result was embarrassing
The core loop is pure Python with hashlib calls, no I/O worth mentioning:
python
from concurrent.futures import ThreadPoolExecutor
import hashlib, timedef work(chunk):
return sum(hashlib.sha256(x.encode()).digest()[0] for x in chunk)
with ThreadPoolExecutor(max_workers=8) as ex:
results = list(ex.map(work, chunks))
Eight workers: 44 minutes. Slower than the single-threaded 41. The GIL means only one thread executes Python bytecode at a time, and all the handoff adds overhead. hashlib releases the GIL for large inputs, but mine were short strings, so it barely helped. I measured with time.perf_counter() around each chunk, and the variance told the story before I read anything about the GIL. One of my colleagues pointed out that top showed one core pegged and the rest idle, which is the classic signature. I'd been blaming the disk because the job reads a lot of small files, but iostat showed nothing interesting. It was the interpreter the whole time.
## Processes fixed the CPU problem
ProcessPoolExecutor with 8 workers brought it to 9 minutes on a 16-core box. Great, until I tried to run it from macOS 14.5 with Homebrew Python 3.12.4:
AttributeError: Can't pickle local object 'main.<locals>.worker'
I'd defined worker inside main() so it could close over a config dict. Pickle needs a module-level import path for functions. Moved it to module scope and passed the config as an argument. Pickle also wants workers importable by name, so a lambda fails the same way. I keep the pool creation in main() now and make everything the workers touch a plain module-level function with simple arguments. Boring, but it runs everywhere.
## Spawn is a different beast
On Linux the default start method is fork, so the first version appeared to work. macOS and Windows default to spawn, which re-imports the module. My test script wasn't guarded:
AttributeError: Can't get attribute 'worker' on <module '__main__' (built-in)>
Everything that touches a process pool has to live under if __name__ == "__main__":. That includes the pool creation, not just the calls. Our Windows 11 CI runner uses Python 3.11.9 and caught this; my Linux laptop didn't, because fork doesn't re-import. I now set multiprocessing.set_start_method("spawn") locally so behavior matches prod.
## What I actually run now
Module-level functions, explicit arguments, a __main__ guard, and chunksize passed to map so we're not pickling one small string at a time. Each worker returns an aggregate rather than raw data, which keeps pickling costs down.
I did try the free-threaded build on 3.13 with PYTHON_GIL=0 python3.13t bench.py. It removed the ceiling but single-thread throughput dropped enough that the 3.12 process pool still won for this workload, and a few dependencies weren't ready. Might revisit it next year.
If you're on I/O-bound work, threads are still fine; the GIL gets released while sockets block. It's only the tight bytecode loops that need processes..