<@ULVA73B9P> How does prefect 2 handle state trans...
# ask-marvin
p
@Marvin How does prefect 2 handle state transition across subflows in a flow DAG? Do I need a global parameter being passed across subflows? Im talking about a scenario like this where
subflow_b
must run after
subflow_a
and relies on state computations from
subflow_a
Copy code
@flow(name="Subflow A")
def subflow_a():
    my_a_task()

@flow(name="Subflow B")
def subflow_b():
    my_b_task()

@flow(name="Main Flow")
def parent_flow():
    subflow_a()
    subflow_b()

if __name__ == "__main__":
    parent_flow()
m
In Prefect 2, you can handle state transitions across subflows in a flow DAG without needing a global parameter. You can pass results from one task or subflow to another within your workflow. This means that
subflow_b
can rely on state computations from
subflow_a
by passing the results as parameters. Here's a modified version of your code to illustrate this:
Copy code
@flow(name="Subflow A")
def subflow_a():
    return my_a_task()

@flow(name="Subflow B")
def subflow_b(param_from_a):
    return my_b_task(param_from_a)

@flow(name="Main Flow")
def parent_flow():
    result_a = subflow_a()
    subflow_b(result_a)

if __name__ == "__main__":
    parent_flow()
In this example,
subflow_a
returns the result of
my_a_task()
, which is then passed as a parameter to
subflow_b
. Remember, it's recommended to write tasks that do a discrete, specific piece of work in your workflow. The more granular you create your tasks, the easier it is to troubleshoot and parallelize your workflow. For more detailed information, you can refer to the Prefect documentation.
1