Parallel Tasks

Run independent tasks concurrently and collect their results.

Runs four independent tasks at once and collects their results in order. The parallel built-in takes already-invoked task coroutines and awaits them together, returning a list positionally matched to the arguments. Reach for it when tasks don’t depend on each other and you want their latencies to overlap instead of stack.

Run it

python examples/parallel_tasks.py
from __future__ import annotations

from flux import ExecutionContext
from flux.task import task
from flux.tasks import parallel
from flux.workflow import workflow


@task
async def say_hi(name: str):
    return f"Hi, {name}"


@task
async def say_hello(name: str):
    return f"Hello, {name}"


@task
async def diga_ola(name: str):
    return f"Ola, {name}"


@task
async def saluda(name: str):
    return f"Hola, {name}"


@workflow
async def parallel_tasks_workflow(ctx: ExecutionContext[str]):
    results = await parallel(
        say_hi(ctx.input),
        say_hello(ctx.input),
        diga_ola(ctx.input),
        saluda(ctx.input),
    )
    return results


if __name__ == "__main__":  # pragma: no cover
    ctx = parallel_tasks_workflow.run("Joe")
    print(ctx.to_json())

Each task is invoked first (say_hi(ctx.input)), then the coroutines are passed to parallel, which awaits them concurrently. The return value is a list in the same order as the arguments, regardless of which task finished first. Every call is still recorded individually, so a replay skips the ones that already completed.

See also


Last verified against Flux 0.56.0.