<@ULVA73B9P> in Prefect 2.X I have a file with two...
# ask-marvin
m
@Marvin in Prefect 2.X I have a file with two flows, I want that inside one of the flows is called the other in a for loop and runned as subflow without waiting so that is run N times
m
In Prefect 2.x, when you want to call a flow inside another flow as a subflow and run it in parallel without waiting for it to finish, you can leverage the asynchronous nature of flows. However, Prefect's default behavior is to wait for subflows to complete before continuing with the parent flow. To achieve parallel execution, you can use Prefect's task concurrency within the flows. Here's a simple strategy for what you're trying to do: 1. Asynchronous Execution: Use Python's
async
capabilities or Prefect's concurrency support to trigger subflows without awaiting their completion immediately. 2. Custom Task Runner: Prefect allows the customization of task runners which could allow more control over how tasks are executed, especially around concurrency and task scheduling. Here's a brief example to give you an idea of how it might look:
Copy code
python
from prefect import flow, task
import asyncio

@task
async def do_something(i):
    # Replace with your actual task logic
    print(f"Task {i} started")
    await asyncio.sleep(1)
    print(f"Task {i} completed")

@flow
async def subflow():
    # This is the subflow called from the parent flow
    for i in range(5):
        await do_something(i)

@flow
def parent_flow():
    asyncio.run(subflow())  # Call the subflow without blocking

if __name__ == "__main__":
    parent_flow()
Key Takeaway: You must manage the concurrency aspect within your Python code using techniques like asyncio or Prefect's own concurrency model. For more detailed information, you can check out the Prefect 2.x documentation on composing flows. Life as a parent flow can sometimes feel like herding cats—just don't tell the subflows!