<@ULVA73B9P> is there a way to run child or sub-fl...
# ask-marvin
m
@Marvin is there a way to run child or sub-flows in parallel within a parent flow?
m
Yes, you can run child or sub-flows in parallel within a parent flow. When you have a flow function called inside the execution of another flow, the primary flow is the "parent" flow, while the flow created within the parent is the "child" flow or "subflow." To run subflows in parallel, you can use AnyIO task groups or
asyncio.gather
, allowing asynchronous subflows to execute concurrently. For more information on composing flows and running them in parallel, you can refer to Prefect's documentation on composing flows.
m
can you create an example of that?
n
Copy code
In [1]: from prefect import flow

In [2]: import asyncio

In [3]: @flow
   ...: async def work():
   ...:     await asyncio.sleep(5)
   ...:

In [4]: @flow
   ...: async def parent():
   ...:     await asyncio.gather(*[work() for _ in range(5)])