<@ULVA73B9P> I can use wait_for in a task to get a...
# ask-marvin
a
@Marvin I can use wait_for in a task to get a bunch of other tasks to complete before starting the next one. Do subflows allow the same thing?
m
thought for 115.5 seconds
Yes — subflows can wait on upstream task futures before they start. In Prefect 3.x, a subflow call supports
wait_for
much like a task does:
Copy code
python
from prefect import flow, task

@task
def step_a():
    print("A done")

@task
def step_b():
    print("B done")

@flow
def child_flow():
    print("child flow started")

@flow
def parent_flow():
    a = step_a.submit()
    b = step_b.submit()

    child_flow(wait_for=[a, b])

parent_flow()
Here,
child_flow
won't begin until both
a
and
b
have finished. A useful distinction: -
wait_for=[...]
= ordering only - passing a future as an argument = ordering + pass the resolved result Example of passing data instead:
Copy code
python
from prefect import flow, task

@task
def make_value():
    return 42

@flow
def child_flow(x):
    print(f"got {x}")

@flow
def parent_flow():
    x = make_value.submit()
    child_flow(x)

parent_flow()
In that case, Prefect waits for
x
and passes its result into the subflow. Rule of thumb: - Use
wait_for
when you just want the subflow to start after something else - Pass futures as parameters when the subflow actually needs the data One caveat: if something in
wait_for
fails, the subflow generally won’t start unless you explicitly handle failure cases. If you want, I can also show: - subflow waiting on another subflow - using
wait_for
with
.submit()
on flows
- how this appears in the UI/dependency graph