Bruno Nirello
12/13/2023, 5:10 PMMarvin
12/13/2023, 5:11 PMtask.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:
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.Bruno Nirello
12/13/2023, 5:12 PMMarvin
12/13/2023, 5:13 PMpython
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.