<@ULVA73B9P> how do I invoke other prefect flows i...
# ask-marvin
n
@Marvin how do I invoke other prefect flows inside a flow?
m
thought for 213.9 seconds
Great question! In Prefect 3.x, there are two main ways to invoke one flow from another: 1) Call a flow directly (subflow) – same process, returns result - Use this when both flows run in the same codebase/worker and you want to block until it finishes. - The child flow shows up as a subflow in the UI.
Copy code
from prefect import flow

@flow
def child_flow(x: int) -> int:
    return x * 2

@flow
def parent_flow():
    result = child_flow(5)  # synchronous; blocks until done
    return result
Notes: - Direct calls create a subflow run and return the actual result (not a future). - For concurrency across multiple subflows, make your parent an async flow and use asyncio gather. 2) Trigger a deployment from a flow – different infra, optional wait - Use this when the other flow is deployed (can run on a different worker/infra) or you want fire-and-forget behavior.
Copy code
from prefect.deployments import run_deployment

@flow
def parent_flow():
    flow_run = run_deployment(
        name="child-flow/my-deployment",      # "<flow_name>/<deployment_name>"
        parameters={"x": 5},
        # timeout=None (default) waits until it finishes
        # timeout=0 returns immediately (fire-and-forget)
        # as_subflow=True (default) links it in the UI as a subflow
    )
    # After completion, check final state
    state = flow_run.state
    if state and state.is_completed():
        # To get the return value, ensure the child flow persists results
        value = state.result()  # fetches persisted result
        return value
    return None
Key differences - Direct call - Synchronous in-process execution - Returns the child’s actual return value - Best for composition and code reuse - run_deployment(...) - Queues a run on the child’s deployment infrastructure - Returns a FlowRun object (metadata/state) - Default behavior waits for completion; set timeout=0 for fire-and-forget - Use when you need independent infra, cancellation, or isolation Caveats - Getting the return value from a deployment-triggered flow requires result persistence on the child flow; otherwise, you can check status but not the value. - Direct subflows block the parent; use async if you need parallelism in-process, or trigger multiple deployments with timeout=0 for external concurrency. Docs and references - Subflows (concepts): Organize flows with subflows - Trigger deployments: Run deployments from code - API: prefect.deployments.run_deployment Follow-up: Are you trying to reuse code in-process (subflows) or orchestrate independent deployed flows (possibly on different workers)? This will help me suggest the best pattern.