how to run a flow and wait for the result and then...
# ask-marvin
v
how to run a flow and wait for the result and then continue the other flow @Marvin
m
To run a flow, wait for its result, and then continue with another flow in Prefect 3.x, you can use the
wait_for_flow_run
function. This function allows you to wait for a specific flow run to complete before proceeding with other operations. Here's a basic outline of how you can achieve this: 1. Run the First Flow: Start by running your initial flow and obtain the flow run ID. 2. Wait for Completion: Use the
wait_for_flow_run
function to wait for the first flow's completion. 3. Continue with the Next Flow: Once the first flow completes, you can proceed to run the next flow. Here's a conceptual example:
Copy code
python
from prefect import flow
from prefect.client import get_client
from prefect.flow_runs import wait_for_flow_run

@flow
def first_flow():
    # Your flow logic here
    return "result of first flow"

@flow
def second_flow():
    # Your flow logic here
    print("Second flow is running after the first flow completed.")

async def run_flows():
    # Run the first flow
    first_flow_run = first_flow.submit()

    # Wait for the first flow to complete
    await wait_for_flow_run(first_flow_run.id)

    # Continue with the second flow
    second_flow()

# Execute the run_flows function
if __name__ == "__main__":
    import asyncio
    asyncio.run(run_flows())
Links for Reference: - wait_for_flow_run in Prefect This example assumes you're using an asynchronous environment to handle the flow runs. If you're using an imperative programming style, you'll need to ensure your environment supports asynchronous execution.
n
🤨
no @Marvin flow doesn't have .submit and why not just sequentially call the flow functions?
m
In Prefect 3.x, if you want to run one flow and then another sequentially, you can simply call the flow functions one after the other. This is a straightforward approach if you don't need to manage asynchronous execution or handle flow states explicitly. Here's how you can do it:
Copy code
python
from prefect import flow

@flow
def first_flow():
    # Logic for the first flow
    print("Running first flow")
    return "result of first flow"

@flow
def second_flow():
    # Logic for the second flow
    print("Running second flow")

def main():
    # Run the first flow and wait for it to complete
    result = first_flow()
    
    # Continue with the second flow
    second_flow()

if __name__ == "__main__":
    main()
This approach works well in an imperative programming style, where you simply want one flow to execute after another without any additional orchestration or asynchronous handling. This method leverages Python's natural sequential execution.
v
much cleaner, why is marvin hallucinating?
n
it needs some help sometimes, 4o is still very much an LLM prone to hallucination. when I have time i try to make the RAG better but even the newest LLMs hallucinate