<@ULVA73B9P> Is there anyway to run two subflows i...
# ask-marvin
l
@Marvin Is there anyway to run two subflows in parallel from a parent flow?
m
Yes, there is a way to run subflows in parallel from a parent flow in Prefect. According to the Prefect documentation, asynchronous subflows can be run in parallel by using AnyIO task groups or
asyncio.gather
. This means that while subflows generally block the execution of the parent flow until they complete, you can perform them asynchronously and concurrently if you structure your parent flow correctly. For more detailed guidance on how to compose your flows in this manner, you can refer to the relevant section of the Prefect documentation here.
l
What if my flow is not an async function?
@Marvin What if my subflow is not an async function?
m
It appears that subflows in Prefect, whether synchronous or asynchronous, will be treated like normal flow runs. A subflow will block the execution of the parent (or main) flow until the subflow's completion. The documentation states that subflows, upon starting, create a new task runner for the tasks within them and when they complete, this task runner is shut down. For asynchronous subflows specifically, one can run them in parallel to the parent flow using AnyIO task groups or
asyncio.gather
. However, it doesn't mention any special treatment needed for subflows that are not asynchronous, which implies that they will simply block the parent flow until they complete. There is no indication that a subflow has to be asynchronous. For more details, you can have a look at the Prefect documentation on composing flows: Prefect Flows.
The Prefect documentation specifies that subflows will block execution of the parent flow until completion, but this constraint applies to synchronous subflows. For asynchronous execution, you can run subflows in parallel using AnyIO task groups or
asyncio.gather
. Unfortunately, it seems that the documentation does not explicitly provide a method for running non-async subflows in parallel. However, it may be possible to work around this limitation by wrapping synchronous subflows