---
title: "First-Win Parallel Steps"
description: "Run several Steps at the same time, keep the first result, and cancel the rest. Use it when the first acceptable result is enough, such as the fastest of several quote services."
canonical_url: "https://docs.superdurable.io/design-patterns/parallel/first-win-parallel-steps/"
source: "docs/content/design-patterns/parallel/first-win.mdx"
locale: "en"
language: "python"
---

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

# First-Win Parallel Steps

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

Run several Steps at the same time, keep the first result, and cancel the rest. Use it when the first acceptable result is enough, such as the fastest of several quote services.

**Flow graph: FirstWinParallelStepsFlow**

**Steps**

1. `InitStep` (start Step): Start several work Steps in parallel. Next: `DoWork`.
2. `DoWork`: Do the work; the first to finish completes the Flow and cancels the rest. Next: the Flow completes.

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

## How it works

**InitStep** starts several `DoWork` executions. Each one sleeps for a random short time, so any of them can finish first. The first decision that Dex commits keeps its result and cancels the sibling `DoWork` executions.

Each branch is its own Step execution, with its own retries, failure route, and durable result. Use separate Steps or child Flows when Dex must retry, cancel, or record results for individual branches. With in-process concurrency inside one `Execute`, such as `Promise.any`, asyncio tasks, or threads, Dex retries the whole `Execute`, so all its calls run again together. It fits idempotent reads that succeed or retry together.

## Core implementation

Each `DoWork` completes and cancels its sibling `DoWork` executions in one decision.

**Python**

```python
class DoWork(Step[int]):
    async def execute(  # type: ignore[override]
        self, context: AsyncContext, input: int
    ) -> StepDecision:
        await asyncio.sleep(random.uniform(0.05, 0.5))
        return graceful_complete(input).with_canceling_sibling_steps(DoWork)


class InitStep(Step[int]):
    def execute(self, context: Context, input: int) -> StepDecision:
        return go_to_many(
            *(StepMovement.of(DoWork, index) for index in range(input))
        )
```

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

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

## Rules and pitfalls

- The cancellation selector cancels only sibling executions of the same Step type. Other branches keep running.
- Each losing execution watches its Step's cancellation signal. It passes the signal to calls that accept cancellation. Otherwise it uses short call timeouts and checks the signal between calls.
- Cancellation is cooperative, and it does not reverse an external effect that already happened. Withdraw a reservation that already took effect with a separate, bounded, idempotent call. A reservation that must be released uses an expiring lease or a durable compensation branch keyed by the business ID.

## Next

- [Critical Section](https://docs.superdurable.io/design-patterns/parallel/critical-section.md)
- [Parallel Steps](https://docs.superdurable.io/design-patterns.md#parallel)
