https://prefect.io logo
c

Chris Vrooman

05/07/2020, 6:09 AM
I have a question about executing the same function multiple times within a flow. Is there a recommended way to configure upstream dependencies so that we can ensure that tasks execute in the right order? There is no data dependency for my use case. Was hoping to avoid redefining a function with a different name. Basic Example:
Copy code
@task
def my_function(x, y):
    print(x+y)

with Flow(name="my_flow") as flow:
    # Run 1st
    my_function(1, 2)
    # Run 2nd
    my_function(3, 4)
    # Run 3rd
    my_function(5, 6)
d

David Ojeda

05/07/2020, 6:42 AM
Hi Chris, One option is to achieve this order is set the upstream tasks on you task call:
Copy code
@prefect.task
def hello(x):
    print('hello', x)

with prefect.Flow('my flow') as flow:
    one = hello(1)
    two = hello(2, upstream_tasks=[one])
    three = hello(3, upstream_tasks=[two])
flow.run()
The other option is to call
two.set_dependencies(…)
which is what the
upstream_task
argument is used for…
j

Jeremiah

05/07/2020, 1:14 PM
^ Indeed, as @David Ojeda wrote, use
set_upstream()
or
set_downstream()
to enforce ordering without passing data.
c

Chris Vrooman

05/07/2020, 5:56 PM
@David Ojeda @Jeremiah Ahh, perfect! I didn’t realize you could pass upstream_tasks directly as an argument to the task. It looks like this solved the problem I was facing. Thank you!
👍 1