Joining and Moving Parallel Agent Work

Lesson 1 of 4

Wait for Every Branch Before Joining

Estimated time: 10 minutes

Wait for every branch before joining

A run that splits work across several branches and joins as answers start arriving can decide on a partial set and never notice the branch that was slow. That branch may carry the one answer that changes the decision.

Count the branches first

Write down how many branches the input set produces before starting any of them. A join that does not know the expected count cannot tell a complete set from a partial one.

Failures are reports too

A branch that fails is not the same as a branch that is still running. Both are missing answers, but only the first will never arrive. Record each branch as answered, failed or pending, so silence is never read as success.

def gather(branches, reports):
    answered = {r["branch"] for r in reports}
    missing = [b for b in branches if b not in answered]
    return {"complete": not missing, "missing": missing}

Do not let speed set the set

The branches that finish first were not chosen to be the important ones. Joining on the fast set quietly gives the decision to whichever answer arrived in time, which is a race rather than a policy.

Label a partial set as partial

Sometimes you must decide before every branch reports. That is allowed, as long as the result says it was built from a partial set, so the reader can weight it. A partial set presented as complete is the failure this guards against.