<@ULVA73B9P> how do I run subflows?
# marvin-ai
p
@Marvin how do I run subflows?
m
thought for 426.6 seconds
Great question! In Prefect 3.x there are two common ways to run subflows. Pick the one that fits your coupling/infrastructure needs: 1) Call a flow from another flow (in‑process, simplest) - Runs the child flow in the same process as the parent - Synchronous: the parent waits for the child to finish - Automatically shows up as a subflow in the UI
Copy code
from prefect import flow, get_run_logger

@flow
def child(x: int) -> int:
    log = get_run_logger()
    <http://log.info|log.info>(f"Child got {x}")
    return x * 2

@flow
def parent():
    result = child(21)  # tracked as a subflow in the UI
    return result

if __name__ == "__main__":
    parent()
2) Trigger a deployed flow as a subflow (separate infrastructure) - The child runs as its own flow run on a worker (can use different resources/pools) - By default, it’s linked as a child run in the UI (
as_subflow=True
) - You pass parameters via a dict
Copy code
from prefect import flow
from prefect.deployments.flow_runs import run_deployment

@flow
def parent():
    # Assumes you've created a deployment for the child flow, e.g. "child-flow/prod"
    child_run = run_deployment(
        name="child-flow/prod",
        parameters={"x": 21},
        as_subflow=True,        # keep parent/child linkage in the UI
        flow_run_name="child-subrun",
    )
    # child_run contains details including the final state
    return child_run.state
Notes - Direct calls (option 1) use the parent’s task runner and run in the same process. - Deployment-based subflows (option 2) run on worker infrastructure; you can control concurrency via deployment settings and concurrency limits. - Both approaches show the parent/child relationship in the UI when
as_subflow=True
(default for
run_deployment
). Docs to dig deeper: - Run deployments (run_deployment) - Deployments concept - Workflow design patterns (overview and when to use each pattern) Quick check: Do you want the subflow to run in the same process (simpler) or on separate infrastructure via a deployment (more isolation/scalability)?