Explanation
What just happened
Calling sha256 is Python code, so it needs the GIL. Inside, hashlib’s C code lets go of the GIL before it hashes, and takes it back after. While it hashes, the other thread is free to run, so both threads hashed at once on two cores.
Try the what-if switch
- 1 KB at a time: the same 64 MB in 64,000 small updates. Each is too small for hashlib to let go of the GIL, so the threads take turns.
Why
The GIL protects Python objects. C code that only touches raw bytes doesn’t need it, so well-written extensions let it go around long work. Letting go and taking it back costs a little, so hashlib only bothers for big inputs.
When to use it
Threads are a fine way to hash, compress or crunch big buffers in parallel, as long as the heavy work happens in C that lets go of the GIL.
Watch out
Whether a library lets go of the GIL, and from what size, is up to the library. Measure before you count on two cores.
Common questions
Can Python threads run in parallel with the GIL?
Yes, while they run C code that has let go of the GIL. Two threads each hashing a 64 MB buffer with hashlib.sha256 keep two CPUs busy on the standard build.
Does hashlib release the GIL?
For big updates, yes. Updates of 64 KB let two threads hash at once; updates of 1 KB keep the GIL, so the threads take turns.
Does it matter on free-threaded Python?
Less. On python3.15t there is no GIL, so even the 1 KB updates run on two cores at once.
Verified: this timeline matches a real program, checked in CI on Python 3.14, 3.15 and free-threaded 3.15t. How we verify. Unclear or wrong? Tell us