Tom Han
09/10/2025, 6:30 PMrun_deployment in a sync function can I expect my return to be a flowrun object?Marvin
09/10/2025, 6:30 PMNate
09/10/2025, 6:32 PMrun_deployment you can do run_deployment(..., _sync=True) and it will always behave like a sync functionMarvin
09/10/2025, 6:32 PMrun_deployment(...) returns a FlowRun object.
- Import path: from prefect.deployments import run_deployment
- Return type: FlowRun
- You can call it from synchronous code and use the returned object’s attributes (e.g., id, state, name).
Example:
from prefect.deployments import run_deployment
flow_run = run_deployment("my-flow/my-deployment", parameters={"x": 1})
print(flow_run.id)
# optionally: print(flow_run.state)
API reference: Prefect 3.x deployments APINate
09/10/2025, 6:34 PMrun_deployment from a sync function, from another parent async function, the @sync_compatible decorator would have run_deployment return a coro, hence confusion articulated by #15008Tom Han
09/10/2025, 6:36 PMMarvin
09/10/2025, 6:36 PMMarvin
09/10/2025, 6:38 PMdef my_flow(...):), it runs synchronously.
- If your flow is async (async def my_flow(...):), it runs in an event loop.
If you call run_deployment from a synchronous flow (even one started from the UI), that call is sync-friendly. It returns a FlowRun object, and you can control whether it waits for the child run to finish with the `timeout`/`poll_interval` parameters. It does not require you to be in an async context.
Example:
from prefect import flow
from prefect.deployments import run_deployment
@flow
def parent():
# treat the child run as a subflow and wait for completion
child = run_deployment(
"my-flow/my-deployment",
parameters={"x": 1},
timeout=None, # wait indefinitely for completion
poll_interval=5,
as_subflow=True # default; shows up nested under the parent
)
return child.state
API reference: Prefect 3.x deployments APITom Han
09/10/2025, 6:49 PMif update_db:
update_flowrun: FlowRun = run_deployment(
name="update-inference-status",
parameters={
"video_path": str(video_path).removeprefix(VAST_PREFIX),
"status": "running",
"model_path": str(model_path).removeprefix(VAST_PREFIX),
"output_path": None,
},
as_subflow=True
)
while not update_flowrun.state.is_final():
time.sleep(0.1)
update_flowrun = FlowRun.load(update_flowrun.id)Marvin
09/10/2025, 6:49 PMNate
09/10/2025, 6:52 PMNate
09/10/2025, 6:52 PMrun_deployment already polls for a terminal stateNate
09/10/2025, 6:53 PMrun_deployment wont return until its in a terminal state unless you say timeout=0Tom Han
09/10/2025, 6:53 PMTom Han
09/10/2025, 6:53 PMNate
09/10/2025, 6:54 PMis_final checkMarvin
09/10/2025, 6:56 PMFlowRun.load(...) in Prefect 3.x, so that line will fail.
2) Prefer the full deployment slug ("flow-name/deployment-name") or the deployment UUID for the name argument, not just the deployment name.
If you want to block until the child run finishes, use wait_for_flow_run or set timeout on run_deployment. Here are two working options:
Option A: Use wait_for_flow_run (recommended)
from prefect.deployments import run_deployment
from prefect.flow_runs import wait_for_flow_run
if update_db:
fr = run_deployment(
name="my-flow/update-inference-status",
parameters={
"video_path": str(video_path).removeprefix(VAST_PREFIX),
"status": "running",
"model_path": str(model_path).removeprefix(VAST_PREFIX),
"output_path": None,
},
as_subflow=True,
)
final = wait_for_flow_run(fr.id, timeout=3600, poll_interval=5)
# final.state is the terminal state
Option B: Poll with the client (sync)
import time
from prefect.deployments import run_deployment
from prefect.client.orchestration import get_client
if update_db:
fr = run_deployment(
name="my-flow/update-inference-status",
parameters={
"video_path": str(video_path).removeprefix(VAST_PREFIX),
"status": "running",
"model_path": str(model_path).removeprefix(VAST_PREFIX),
"output_path": None,
},
as_subflow=True,
)
with get_client(sync_client=True) as client:
while True:
fr = client.read_flow_run(fr.id)
if fr.state.is_final():
break
time.sleep(1)
Tip: run_deployment can also handle waiting itself via the timeout parameter if you want it to block; otherwise it returns the FlowRun immediately and you can poll with one of the approaches above.
Docs:
- run_deployment: Prefect 3.x deployments API
- wait_for_flow_run: Prefect 3.x flow_runs APIBring your towel and join one of the fastest growing data communities. Welcome to our second-generation open source orchestration platform, a completely rethought approach to dataflow automation.
Powered by