Hi all, I have a Python task in my flow that has s...
# ask-community
z
Hi all, I have a Python task in my flow that has sys.exit() in it meaning I want that task to break if some condition has met and do not proceed further. But, my task keeps on running in this case and does nothing. Any help?
s
You should be able to use something like
Copy code
from prefect.engine.signals import FAIL
from prefect import task

@task
def my_task():
   if condition:
        raise FAIL("Fail Message")
This will have the task enter a failed state.
There are some others signals that might be useful in some cases, see here.
upvote 2
z
would it retry in case of fail?
s
I believe it would if the task's parameters allow it to.
e.g.
max_retries=1
z
right, thanks.. will try this
even for Success, I can do raise SUCCESS("Message")?
s
Theoretically you can do that, yeah. It think it is not recommended though and that just having your task return something is a more "prefectic way". Actually, you could also have your task fail by raising any error that you would not catch e.g.
Copy code
@task
def my_task:
   if condition:
       raise AttributeError("something went wrong")
a
@Zohaa Qamar As @Sylvain Hazard mentioned, the best way to solve it, would be to raise a signal that would end the task run execution. Raising FAILED signal respects retries. If you don’t want retries to be triggered, use ENDRUN:
Copy code
from prefect.engine.signals import ENDRUN
from prefect import task

@task
def my_task():
   if condition:
        raise ENDRUN("Exiting the task execution...")
z
ENDRUN would stop the whole flow execution or would just stop that task and move to next ones?
a
it would stop the task. If you want to stop the entire flow, the easiest way to do it would be adding a conditional logic:
Copy code
from prefect import task, Flow, case

@task(log_stdout=True)
def end_flow():
    print("Ending the flow run due to the XXX")

with Flow("conditional-logic") as flow:
    cond = check_condition()

    with case(cond, True):
        val = t1()
        val2 = t2()
        t3(val, val2)

    with case(cond, False):
        end_flow()
z
thanks!