---
title: "Dynamic Parallel Steps"
description: "Run the same Step type once per item, at the same time. Use it when the number of branches is known only at runtime, such as one branch per line of an order."
canonical_url: "https://docs.superdurable.io/design-patterns/parallel/dynamic-parallel-steps/"
source: "docs/content/design-patterns/parallel/dynamic.mdx"
locale: "en"
language: "python"
---

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

# Dynamic Parallel Steps

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

Run the same Step type once per item, at the same time. Use it when the number of branches is known only at runtime, such as one branch per line of an order.

**Flow graph: DynamicParallelStepsFlow**

**Steps**

1. `InitStep` (start Step): Start as many work Steps as the input asks for. Next: `DoWork`.
2. `DoWork`: Do one unit of work. Next: the Flow completes.

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

## How it works

**InitStep** builds one `DoWork` movement per input item. Dex runs those executions concurrently. Each one sleeps for a random short time, so they finish in any order.

Pass each branch the key of its item, so every result maps back to its input. The active Step executions of one Flow run are [limited](https://docs.superdurable.io/reference/server/configuration.md#step-and-channel-limits), and waiting Steps count too. Batch a fan-out that can grow large, or split it across [SubFlows](https://docs.superdurable.io/design-patterns.md#subflows).

## Core implementation

**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)


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/dynamic_parallel_steps_flow.py](https://github.com/superdurable/dex/tree/main/examples/python/dex_examples/patterns/parallel/dynamic_parallel_steps_flow.py)

## Next

- [Await Parallel Steps](https://docs.superdurable.io/design-patterns/parallel/await-parallel-steps.md)
- [Parallel Steps](https://docs.superdurable.io/design-patterns.md#parallel)
