https://prefect.io logo
Title
i

itay livni

10/10/2019, 8:50 PM
Hi - Is there a way to
pass
on a if else branch?
j

Jeremiah

10/10/2019, 11:52 PM
Hi @itay livni, could you give a little more detail on what you want your workflow to do when that
pass
happens? You can absolutely make a task that does nothing but
pass
, in which case it will return a
Success
state to any downstream tasks containing no result.
i

itay livni

10/11/2019, 12:05 AM
something like this...
j

Jeremiah

10/11/2019, 1:34 AM
I think I see — you’ll need to change how you write code in Prefect. The code you’ve written is as if your task results are known (for example
x_lst.value_count().max()
). In Prefect, task results are not known until the flow is run, and consequently all operations — including things like
some_str is "short"
— must happen inside the
run()
method of a
Task
object.
So with
ifelse
, you’re saying “if the first task evaluates
True
, run the second task; otherwise run the third task”:
ifelse(conditional_task, true_task, false_task)
Then you can follow that with new task dependencies:
another_task_on_true_branch.set_upstream(true_task)
another_task_on_false_branch.set_upstream(false_task)
And the ifelse will skip all the tasks that don’t get run
i

itay livni

10/11/2019, 3:32 AM
so the test statement i.e
termsense_stat_response["min_num_term_senses"]>1
has to be a task? Here is an implementation of what I am trying to do in an aws state machine
sorry wrong one
j

Jeremiah

10/11/2019, 3:45 AM
Yes, every operation in Prefect must be a
Task
. In this case, you could use an
ifelse
to run
task1
if the condition is true, or
task2
if the condition is false. You could also call
task2.set_upstream(task1)
. This way task 2 will always run, but task 1 will only run if the condition is met.