I have a question about executing the same functio...
# prefect-community
c
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
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
^ Indeed, as @David Ojeda wrote, use
set_upstream()
or
set_downstream()
to enforce ordering without passing data.
c
@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