12/34
Part 4 of 8 · PoolsLearn and do
A barrier with a timeout proves that the workers overlap
Correct results don't show that the tables ran at the same time. A test can show that too.
def test_tables_run_at_the_same_time():
barrier = threading.Barrier(3, timeout=2)
lock, first = threading.Lock(), set()
# each table holds one value, k
def meet(value):
with lock:
# the first value of this table?
arrived = value not in first
first.add(value)
if arrived and value < 3:
# tables 0, 1 and 2 must all be running
barrier.wait()
return value
tables = [[[k, k], [k, k]] for k in range(6)]
assert run_batch(tables, [("map", meet)], workers=3) == tables
assert not barrier.broken
A batch that runs one table at a time returns exactly the right results, so checking the results can't catch it. This test makes the workers prove it from inside.
A threading.Barrier(3) blocks each thread that calls wait() until 3 threads are waiting, then lets them all go. With a timeout, a barrier that never fills raises BrokenBarrierError instead of hanging.
Barrier(3, timeout=2), but the batch runs one table at a time. What does table 0's wait() do?
Pick an answer above to see the rest