<@ULVA73B9P> how to set task dependency in prefect...
# ask-marvin
b
@Marvin how to set task dependency in prefect 2.0?
m
In Prefect 2.0, you define task dependencies using the functional API approach rather than the imperative methods from Prefect 1.x, such as
task.set_upstream()
,
task.set_downstream()
, or
flow.set_dependencies()
. The Prefect 2.0 functional API allows you to set dependencies by using the standard Python function invocation style, where tasks can be treated as Python functions that return their results. Here's an example of setting task dependencies in Prefect 2.0:
Copy code
python
from prefect import flow, task

@task
def task_1():
    # Logic for task 1
    return result_1

@task
def task_2(upstream_result):
    # Logic for task 2 that depends on task 1
    return result_2

@flow
def my_flow():
    result_1 = task_1()
    # Task 2 will run after Task 1 and use its result as an input
    result_2 = task_2(result_1)
    
my_flow()
In this example,
task_1
does not have any dependencies.
task_2
, however, depends on the output of
task_1
. When defining the flow
my_flow
, calling
task_1()
will execute the task, and its result is then passed as an argument to
task_2()
. This defines a dependency where
task_2
will run only after
task_1
completes and it will utilize the result of
task_1
. For more detailed information on migration from Prefect 1.0 to 2.0 and the usage of the functional API for setting task dependencies, please refer to the Prefect Migration Guide.
b
@Marvin ok, but why if my task don't have a return?
m
If your task does not return a value, you can still create dependencies between tasks in Prefect 2.0 by simply invoking them in the order you want them to run within the flow. Prefect will recognize that one task was called after another and will ensure they run in the specified sequence, even if they do not pass data between them. Here is an example of how you can set dependencies between tasks that don't return values:
Copy code
python
from prefect import flow, task

@task
def task_1():
    # Logic for task 1
    print("Task 1 is running")

@task
def task_2():
    # Logic for task 2 that depends on task 1
    print("Task 2 is running after Task 1")

@flow
def my_flow():
    # Invoke task_1; this task doesn't return any value
    task_1()
    # Invoke task_2; Prefect understands that this should run after task_1
    task_2()
    
my_flow()
In this example,
task_1
is executed first. After
task_1
completes,
task_2
is invoked. Prefect handles the execution order for you under the hood, maintaining the dependency rule that
task_2
should run after
task_1
. This is similar to creating explicit dependents without passing data. The execution order of tasks within the flow defines their dependency relationship, and Prefect makes sure to respect that order when scheduling task runs.