Tri
10/28/2024, 4:22 PMBianca Hoch
10/28/2024, 10:18 PM@task(retries=3)
) .
If you want to skip a failed task and continue on with the flow, you could:
a) Use a try/except block within your flow to catch and handle task failures.
from prefect import flow, task
@task
def my_task():
raise ValueError()
@flow
def my_flow():
try:
my_task()
except ValueError:
print("Oh no! The task failed.")
return True
my_flow()
b) Use the return_state=True
option when calling a task, which allows you to check the task's state and decide how to proceed.
from prefect import flow, task
@task
def my_task():
raise ValueError()
@flow
def my_flow():
state = my_task(return_state=True)
if state.is_failed():
print("Oh no! The task failed. Falling back to '1'.")
result = 1
else:
result = state.result()
return result + 1
result = my_flow()
assert result == 2