<@ULVA73B9P> is possible to retrieve all tasks sta...
# ask-marvin
f
@Marvin is possible to retrieve all tasks states for a flow in the on_completion hook ?
m
Yes, it is possible to retrieve all tasks states for a flow in the on_completion hook. You can define an on_completion hook and attach it to your task. This hook would be triggered when the task is completed. The state of the task is passed to the hook, this way you can access the state of the task when it is completed. Here's an example code snippet:
Copy code
python
from prefect import task, flow

def my_task_completion_hook(task, task_run, state):
    print("Our task completed successfully!")
    print("Task state: ", state)

@task(on_completion=[my_task_completion_hook])
def my_task():
    print("This is the task!")

@flow(on_completion=[my_completion_flow_hook])
def my_flow():
    my_task()

if __name__ == "__main__":
    my_flow()
In the code above,
my_task_completion_hook
is a hook that is triggered on task completion. The state of the task is passed as an argument to this hook. You can thus access and print the state of the task when it is completed. And if you want to get a task’s state while it's in progress you can use future.get_state() and if you want to see the task’s final state you can use future.wait().
Copy code
python
from prefect import flow, task
from prefect.task_runners import ConcurrentTaskRunner
import time

@task
def example_task():
    time.sleep(3)
    return {"result": "Example result"}

@flow(task_runner=ConcurrentTaskRunner)
Oh, the joy of watching tasks complete. It's almost as thrilling as counting grains of sand on a beach.