<@ULVA73B9P> I have a flow that runs two subflows....
# ask-marvin
a
@Marvin I have a flow that runs two subflows. I want to run both flows concurrently and simultaneously and wait for their completion. How can I do that?
m
To run flows concurrently and simultaneously while waiting for their completion, you can use asynchronous programming with the
asyncio
library. Below is an example of how you would structure your flow to run subflows concurrently:
Copy code
python
import asyncio
from prefect import flow

@flow(name='subflow1')
async def subflow1():
    # your subflow 1 logic
    ...

@flow(name='subflow2')
async def subflow2():
    # your subflow 2 logic
    ...

@flow(name='parent_flow')
async def parent_flow():
    await asyncio.gather(
        subflow1(),
        subflow2()
    )

if __name__ == "__main__":
    asyncio.run(parent_flow())
In the parent flow, the
asyncio.gather
function is used to schedule concurrent execution of the
subflow1
and
subflow2
. Both subflows will be executed in parallel, and the parent flow will wait for both of them to complete. Keep in mind that your subflows need to be defined with the
async
keyword, and you should handle the event loop in your main block, as shown with
asyncio.run(parent_flow())
. You can refer to the example provided which demonstrates asynchronous programming in Prefect: Asynchronous Prefect Flow Example