Thomas DeBroux
09/04/2025, 1:41 PMMarvin
09/04/2025, 1:41 PMMarvin
09/04/2025, 1:50 PMfrom prefect import task, flow
@task
def child(x):
return x + 1
@task
def parent(x):
# This schedules another task run from inside a task
fut = child.submit(x)
return fut.result()
@flow
def my_flow(x):
return parent(x)
- Better: orchestrate from the flow
from prefect import task, flow
@task
def child(x):
return x + 1
@task
def parent_logic(x):
# pure logic or lightweight work only
return x * 2
@flow
def my_flow(x):
a = parent_logic.submit(x)
b = child.submit(a)
return b.result()
- Or factor shared logic into a plain function
def compute(x):
return x + 1
@task
def child(x):
return compute(x)
@task
def parent(x):
# Reuse the same logic without creating nested task runs
return compute(x)
- Use subflows for larger units
from prefect import flow, task
@flow
def child_flow(x):
return x + 1
@flow
def parent_flow(x):
return child_flow(x) # a separate subflow run
If you can share which Prefect version you’re on, I can tailor the guidance to that version. If you want, drop a snippet of the “nested task” you’re seeing and I’ll suggest the cleanest refactor.
References
- Workflow design patterns with Prefect
- Prefect 3 GA announcement (covers execution model and design goals)
- Task runners and concurrency concepts: Task runners in Prefect 3 and Running work concurrently