Skip to content

.tip

· one of the small things

A barrier makes race conditions reproducible

Threads that start at slightly different times rarely collide, so the test passes and production doesn't. A barrier makes them all hit the same line at the same moment.

barrier = threading.Barrier(16)
def worker():
    barrier.wait()
    for _ in range(50_000): record("invoice")

Assert on the total. On the free-threaded build this fails against a naive counter every single time, and that's the whole point.

pythontestingconcurrency

Longer version: the post this came from.