Python threads that do run in parallel: C code that releases the GIL

Read the explanation
What if

Two threads hash a 64 MB buffer each. hashlib's C code lets go of the GIL while it hashes, so both hash at once on two cores.

Predict first

Standard Python, GIL on. Two threads each hash a 64 MB buffer with hashlib.sha256. How many cores do they keep busy?

0123456789start a, bjoin()returnthread a: call sha256callhash 64 MB in C, GIL releasedprintthread b: call sha256callhash 64 MB in C, GIL releasedprintheld by aheld by bheld by aheld by b

Tap any block to see what that thread is doing.

  • Running
  • Waiting / parked
  • Holding lock
Step 1/6

main starts two threads. Each hashes the same 64 MB buffer with hashlib.

1DATA = b"x" * 64_000_000  # 64 MB2 3def digest(name):4    h = hashlib.sha256(DATA)  # the hashing runs in C5    print(f"{name} done: {h.hexdigest()[:8]}")6 7def main():8    a = threading.Thread(target=digest, args=("a",))9    b = threading.Thread(target=digest, args=("b",))10    a.start()11    b.start()12    a.join()13    b.join()14 15if __name__ == "__main__":16    main()

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

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