Skip to main content

Wait for Half

View as Markdown

Start N SubFlows, wait for half to finish, then stop the unfinished children.

Each child runs in its own SubFlowStep execution. A completed child publishes one message to SubFlowCompletedCh. WaitSubFlows waits for half of those messages, then publishes one AllDoneCh message for every possible unfinished child.

Once half of the children have finished, each waiting SubFlowStep consumes one AllDoneCh message and tries to stop its child. The Step records the outcome in ChildStopOutcomes, keyed by the child Flow ID, then completes gracefully. A successful stop records stopped. A typed error for a missing, inactive, or closing Flow prompts a state read. The Step records the terminal status and any stored result for a closed child. A running, closing, or missing child is recorded as unknown for a later read. Other errors trigger a Step retry.

Core implementation

wait_for_half_parent_flow.pyPython
@dataclass(frozen=True)
class ChildStopOutcome:
status: str
result: str | None = None


CHILD_STOP_OUTCOMES = AttributeMap("ChildStopOutcomes", ChildStopOutcome)


class WaitForHalfInit(Step[list[str]]):
def execute(self, context: Context, requests: list[str]) -> StepDecision:
if not requests:
return graceful_complete()
return go_to_many(
StepMovement.of(WaitSubFlows, len(requests)),
*(StepMovement.of(SubFlowStep, request) for request in requests),
)


class SubFlowStep(Step[str]):
def __init__(
self,
client_provider: Callable[[], AsyncClient],
example_subflow: ExampleSubFlow,
subflow_completed_ch: Channel[bool],
all_done_ch: Channel[bool],
) -> None:
self.client_provider = client_provider
self.example_subflow = example_subflow
self.subflow_completed_ch = subflow_completed_ch

Example: examples/python/dex_examples/patterns/parallel-subflows/wait_for_half_parent_flow.py

Children that finish at the threshold

When the report counts every child or a follow-up action depends on how each child ended, the Step reads the child after every stop and records how it ended. When the parent needs only the quorum, it stops the remaining children and moves on without this read. A child can finish at the same moment its stop message arrives, and a Channel condition wins over a SubFlow condition when both are ready, so the Step may stop a child that already ended. After a successful stop, or a refused one such as "not found", "not active", or "closing", read the child through the Client. Record a closed child's terminal status and stored result, so the report counts every child that finished. Read a child that is still running or closing again later. A child that is not found is recorded by business policy, such as "unavailable". Return any other stop or read error so the Step retries. Record every child, finished or stopped, in one record keyed by its SubFlow ID. The example is simplified: it does not read the child's terminal state after a successful stop.

When each child needs its own business identity, start it as a top-level Flow instead. The quorum Step stops each child that has not reported and, when the parent needs its outcome, reconciles it by the rules on that page. Normal reports and reconciled outcomes go into one record, keyed by the child Flow ID.

Next