Skip to main content

Dynamic Parallel Steps

View as Markdown

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.

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, and waiting Steps count too. Batch a fan-out that can grow large, or split it across SubFlows.

Core implementation

dynamic_parallel_steps_flow.pyPython
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

Next