Marvin
09/17/2024, 2:21 PMCompleted
or Failed
based on some condition:
python
from prefect import task, flow
from prefect.states import Completed, Failed
@task
def my_task(condition):
if condition:
return Completed(message="Task completed successfully")
else:
return Failed(message="Task failed")
@flow
def my_flow():
state = my_task(condition=True)
print(state)
if __name__ == "__main__":
my_flow()
Reference: Prefect 2.0: States Documentation
### For Prefect 3.x:
The approach is quite similar. You can set the state by returning a Completed
or Failed
state object from the task.
python
from prefect import task, flow
from prefect.states import Completed, Failed
@task
def toggle_task(fail: bool):
if fail:
return Failed(message="I was instructed to fail.")
else:
return Completed(message="I was instructed to succeed.")
@flow
def example():
state_one = toggle_task(fail=True)
print(state_one)
if __name__ == "__main__":
example()
Reference: Prefect 3.0: Manage States
Feel free to try out the examples based on your Prefect version, and let me know if you have any other questions!