Koen
02/07/2025, 10:46 AMMarvin
02/07/2025, 10:46 AM.submit()
in Prefect 3.x, you can access the state
attribute of the PrefectFuture
object returned by the submit
method. When you use .result(raise_on_failure=False)
, it will return the result without raising an exception if the task fails, but you can still check the state to determine if it was successful or not.
Here's a concise way to do it:
python
from prefect import task, flow
@task
def my_task():
# Task logic here
return "task result"
@flow
def my_flow():
future = my_task.submit()
result = future.result(raise_on_failure=False)
task_state = future.state # Access the state of the task
if task_state.is_failed():
print("Task failed.")
elif task_state.is_completed():
print("Task completed successfully.")
else:
print("Task is in a different state.")
my_flow()
This example checks if the task is failed or completed by examining the state of the future object. You can adjust the checks based on other states like is_crashed()
or is_cancelled()
if needed.
For more information, you can explore the Prefect tasks documentation and the states documentation.