Back to examples
Graph Patterns
Fan-Out
One source task splits into three parallel branches.
When to use
Use fan-out when a single piece of data needs to be processed independently by multiple downstream tasks in parallel.
graph-patterns/fan-out.py
"""
01_fan_out.py: One source task splits into three parallel branches.
Demonstrates:
- Fan-out: a single upstream result feeds multiple independent downstream tasks
- DAGY schedules the three branches in parallel (max_workers controls concurrency)
"""
from dagy import flow, task
@task
def load_dataset(name: str) -> dict:
return {"name": name, "rows": list(range(30))}
@task
def compute_mean(dataset: dict) -> float:
rows = dataset["rows"]
return sum(rows) / len(rows)
@task
def compute_max(dataset: dict) -> int:
return max(dataset["rows"])
@task
def compute_min(dataset: dict) -> int:
return min(dataset["rows"])
@flow(name="fan_out_flow")
def fan_out_flow(dataset_name: str = "sales_q1") -> None:
dataset = load_dataset(dataset_name)
# All three tasks depend on `dataset` but not on each other, so they run in parallel.
compute_mean(dataset)
compute_max(dataset)
compute_min(dataset)
if __name__ == "__main__":
result = fan_out_flow.run_local(max_workers=3)
print(f"Run ID : {result.run_id}")
print(f"Status : {result.status}")How it works
- `load_dataset` produces a dataset dict with rows.
- Three tasks (`compute_mean`, `compute_max`, `compute_min`) each receive the same dataset.
- Since none depends on another, DAGY schedules all three in parallel.
- `max_workers=3` allows all three to run concurrently.
Inputs
- `dataset_name` (str, default `"sales_q1"`): the dataset to load.
Outputs
- Mean, max, and min values computed in parallel.
Dependencies
`dagy`