---
title: "Await Parallel Steps"
description: "Continue only after every parallel branch has finished. Use it when a later Step needs the results of all branches, such as combining them into one reply."
canonical_url: "https://docs.superdurable.io/design-patterns/parallel/await-parallel-steps/"
source: "docs/content/design-patterns/parallel/await.mdx"
locale: "en"
language: "python"
---

> Index of every page for AI agents: https://docs.superdurable.io/llms.txt

# Await Parallel Steps

Code on this page is in Python. Other languages: [Go](https://docs.superdurable.io/design-patterns/parallel/await-parallel-steps.go.md) · [Java](https://docs.superdurable.io/design-patterns/parallel/await-parallel-steps.java.md) · [TypeScript](https://docs.superdurable.io/design-patterns/parallel/await-parallel-steps.typescript.md) · [Rust](https://docs.superdurable.io/design-patterns/parallel/await-parallel-steps.rust.md)

Continue only after every parallel branch has finished. Use it when a later Step needs the results of all branches, such as combining them into one reply.

**Flow graph: AwaitParallelStepsFlow**

**Steps**

1. `InitStep` (start Step): Start one work Step per item and the Step that awaits them. Next: `Await`, or `DoWork`.
2. `Await`: Wait for every work Step to report, then complete. Next: the Flow completes. Reads `parallel-complete`.
3. `DoWork`: Do one unit of work and report that it finished. Next: this branch ends. Publishes to `parallel-complete`.

Example: [examples/python/dex_examples/patterns/parallel/await_parallel_steps_flow.py](https://github.com/superdurable/dex/tree/main/examples/python/dex_examples/patterns/parallel/await_parallel_steps_flow.py)

<a id="how-the-join-works"></a>

## How the Join works

`InitStep` forks one round of the **parallel-complete** Join. The fork starts one `DoWork` execution per item and the **Await** Step in the same decision. All of them get one round token: the round key and the branch count. Each `DoWork` sleeps for a random short time, then ends its branch and reports its index. **Await** waits in `WaitFor` for every branch of the round.

A **Join** is a named ChannelMap of branch results. It counts the results that branches report, rather than watching the branch Steps. Results are durable, so a result reported before **Await** starts waiting still counts. Results count only toward their own round. The join Step's wait is an ordinary condition, so it combines with a Timer.

When the work runs in child Flows, a **SubFlow** wait with `AllOf` is the native join.

## Core implementation

**Python**

```python
@dataclass(frozen=True)
class WorkItem:
    index: int
    round: JoinRound


def build_work_item(index: int) -> Callable[[JoinRound], StepMovement[WorkItem]]:
    return lambda join_round: StepMovement.of(DoWork, WorkItem(index, join_round))


class DoWork(Step[WorkItem]):
    def __init__(self, work_completed: Join[int]) -> None:
        self.work_completed = work_completed

    async def execute(  # type: ignore[override]
        self, context: AsyncContext, input: WorkItem
    ) -> StepDecision:
        await asyncio.sleep(random.uniform(0.05, 0.5))
        return self.work_completed.end_branch(context, input.round, input.index)


class Await(Step[JoinRound]):
    def __init__(self, work_completed: Join[int]) -> None:
        self.work_completed = work_completed

    def wait_for(self, context: Context, input: JoinRound) -> Wait:
        return Wait.until(self.work_completed.for_all_branches(input))

    def execute(self, context: Context, input: JoinRound) -> StepDecision:
        return graceful_complete(
            len(self.work_completed.branch_results(context, input))
        )


class InitStep(Step[int]):
    def __init__(self, work_completed: Join[int]) -> None:
        self.work_completed = work_completed

    def execute(self, context: Context, input: int) -> StepDecision:
        return go_to_many(
            *self.work_completed.fork(
                context,
                [build_work_item(index) for index in range(input)],
                lambda join_round: StepMovement.of(Await, join_round),
            )
        )
```

Example: [examples/python/dex_examples/patterns/parallel/await_parallel_steps_flow.py](https://github.com/superdurable/dex/tree/main/examples/python/dex_examples/patterns/parallel/await_parallel_steps_flow.py)

<a id="rules"></a>

## Rules and pitfalls

- Start the join Step in the same fork as the branches. With the implicit round key, fork a Join at most once in one Step method. Explicit round keys and RPC forks are in the [Join reference](https://docs.superdurable.io/reference/server/data-primitive-semantics.md#join).
- Each branch reports exactly one result per round, on every exit path that leaves the Flow open. That includes the Step that a failure route goes to. A branch that never reports leaves the join waiting forever. A branch that reports twice counts twice.
- A branch of several Steps passes the round token to its last Step, and only that Step reports.
- Only the join Step waits for its round; branches move on.
- A canceled branch stays silent, so cancel the join together with the branches it waits for, or wait for the join and a Timer with `AnyOf`.
- Read a round's pending results through the Join's ChannelMap, for example in an RPC. Never publish to it directly.
- A join Step canceled after its wait consumed results follows [the canceled Step rule](https://docs.superdurable.io/reference/server/data-primitive-semantics.md#canceled-step-messages) for those results.

## Wait for some branches

The join Step can also wait for the first branch or for n branches. Then it either cancels the other branches or lets them run on.

To cancel them, the join Step returns a decision that cancels its sibling Steps. Fork starts the branches and the join together, so the siblings are the Steps that fork started. They do not include later Steps of a multi-Step branch, and an RPC fork needs a cancel by Step type; see the [Join reference](https://docs.superdurable.io/reference/server/data-primitive-semantics.md#join). Each branch handles its Step's cancellation signal as in [First-Win Parallel Steps](https://docs.superdurable.io/design-patterns/parallel/first-win-parallel-steps.md). A cancelling join's round is never reset, because its canceled branches never report. In a loop, the next round forks a new round directly.

When the other branches run on, their results arrive later. In a loop, the next round's split Step resets the previous round first: its `WaitFor` waits for the remaining results, and its `Execute` can read them. Then it forks the next round. Without a reset, late results stay pending until the Flow closes.

The [Join reference](https://docs.superdurable.io/reference/server/data-primitive-semantics.md#join) has the edge cases. The [Workflow Patterns evaluation](https://docs.superdurable.io/evaluate-dex/correctness-and-expressiveness/workflow-patterns-evaluation.md#wcp-30) links runnable examples of both shapes.

## Next

- [First-Win Parallel Steps](https://docs.superdurable.io/design-patterns/parallel/first-win-parallel-steps.md)
- [Parallel Steps](https://docs.superdurable.io/design-patterns.md#parallel)
