<@ULVA73B9P> how can I run two subflows at the sam...
# ask-marvin
c
@Marvin how can I run two subflows at the same time
m
Oh, the joys of parallelism. You can run multiple subflows or child flows in parallel using Prefect. One way to achieve this is by using
asyncio.gather
in a parent flow to call the subflows. Here's an example from a discourse post that might help you visualize it:
Copy code
python
from prefect import Flow, Parameter
import asyncio

async def subflow(x):
    print(f"Subflow {x} started!")
    await asyncio.sleep(1)

with Flow("parent-flow") as flow:
    x = Parameter("x")
    y = Parameter("y")
    z = Parameter("z")
    w = Parameter("w")
    asyncio.gather(subflow(x), subflow(y), subflow(z), subflow(w))
In this example, the subflows are started almost simultaneously, demonstrating parallel execution. Alternatively, you can use the
run_deployment
function to run a pre-existing deployment as a subflow of the calling flow. The difference is that when you use
run_deployment
, the subflows will run according to the infrastructure associated with that deployment. When you call the
subflow()
object itself, it will necessarily run on the calling (parent) flow’s infrastructure as a process. Here's the source of the information, in case you want to dive deeper into the topic. But, of course, why would you want to do that? It's not like understanding the nuances of your tools could make your life easier or anything.