Hi <@ULVA73B9P> I want to run a flow from anothe...
# ask-community
k
Hi @Marvin I want to run a flow from another flow, I am able to do it But the catch is, my main flow should not wait until the child flow finishes, it needs to show the status as completed/ success/fail . After it triggers the child flow. I can't use automations option as I need to trigger child only in few conditions of the parameters passed from deployment. How can this be achieved any guidance please
m
I can help you with that! But first, I need to determine which version of Prefect you are using to provide the most accurate guidance. Could you please let me know if you are using Prefect 2.x or Prefect 3.x? If you're not sure, any details about your setup may help me figure it out.
k
I am using prefect 2.0 @Marvin It would be great, if I can trigger a flow from another and exit without waiting for the second flow completion
m
In Prefect 2.x, to run a child flow without waiting for its completion, you can use the combination of "subflows" and asynchronous programming techniques. Prefect itself doesn't block on subflows, and you can proceed with completing your main flow after triggering the child flow. Here's a basic idea of how you can achieve that: 1. Trigger the Child Flow Asynchronously: You can use the
run_deployment
function to start the child flow asynchronously. This function returns immediately, without waiting for the child flow to complete. 2. Use Prefect's
Future
: Prefect uses a similar idea to Python's
Future
to handle asynchronous tasks and subflows. You can submit the child flow as a task and immediately exit the main flow, marking it as completed. Here's a simple example in Prefect 2.x to guide you:
Copy code
python
from prefect import flow, task
from prefect.deployments import run_deployment

@task
def trigger_child_flow():
    run_deployment(name="my-child-flow-deployment")
    return "Child flow triggered"

@flow
def main_flow():
    result = trigger_child_flow.submit()
    print("Main flow completed without waiting for child flow.")

# Run the main flow
main_flow()
In this example,
trigger_child_flow
runs the child flow and the main flow exits right after triggering it. The
run_deployment
is used for triggering another flow without waiting for its completion. For more details, you can refer to the Prefect 2.0 Flow Concepts Documentation. Just like Marvin, you're trying to find the meaning of "life" in flows, aren't you? But worry not, this hack shall deliver enlightenment!
k
Hi @Marvin what if I am using the prefect 3.x
m
In Prefect 3.x, you can trigger a flow from another flow without waiting for the child flow to complete using asynchronous programming concepts that Prefect supports. Here's how you can set this up: 1. Use Asynchronous Functions: Prefect 3.x supports asynchronous execution for flows. By using
async
and
await
, you can manage the execution of multiple flows concurrently. 2. Trigger the Child Flow: You can trigger the child flow as an asynchronous task and proceed with the main flow's execution. Here's a basic example to illustrate asynchronous calls with Prefect 3.x:
Copy code
python
from prefect import flow, task
from prefect.deployments import run_deployment

@task
async def trigger_child_flow():
    await run_deployment(name="my-child-flow-deployment")
    return "Child flow triggered"

@flow
async def main_flow():
    await trigger_child_flow()
    print("Main flow completed without waiting for child flow.")

# Execute the main flow
await main_flow()
In the example, placing
await
ensures that the code is prepared to run asynchronously, although for actually non-blocking behavior in your main execution you might adjust it further based on your broader async setup. For detailed information, you can refer to the Prefect 3.x Flow Writing Documentation. The universe is vast and filled with asynchronous possibilities, much like the mind of a paranoid android grappling with the mysteries of non-blocking execution!