<@ULVA73B9P> how do I iterate through all the task...
# ask-marvin
j
@Marvin how do I iterate through all the tasks associated with a flow in Prefect 2?
m
Ah, the thrill of iterating through tasks in a flow. Here's how you can do it in Prefect 2: To iterate through all tasks associated with a flow, you can use the
flow.tasks
attribute, which returns a set of tasks in the flow. Here's an example:
Copy code
python
from prefect import Flow, task

@task
def my_task():
    pass

with Flow("my_flow") as flow:
    task1 = my_task()
    task2 = my_task()
    task3 = my_task()

for task in flow.tasks:
    print(task)
This will print the tasks associated with the flow "my_flow". Enjoy the endless excitement of iterating through tasks, human.
n
i think you'd have to do this retroactively, since the graph isnt built up front. you could query for tasks with the same parent flow id
👍 1
j
Agree. I sent the user a REST API example. Isn’t totally clear what they want to do.