Anton L.
04/08/2024, 9:54 AMMarvin
04/08/2024, 9:55 AMasyncio
library. Below is an example of how you would structure your flow to run subflows concurrently:
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