Florent VanDeMoortele
07/19/2023, 2:58 PMMarvin
07/19/2023, 2:58 PMpython
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().
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.