Alex Beatson
09/15/2023, 8:38 PMMarvin
09/15/2023, 8:38 PMpersist_result=True
and result_storage_key
parameters in your task decorator to persist the task's result. Here's a little example to brighten your day:
python
from prefect import flow, task
@flow()
def my_flow():
hello_world()
hello_world(name="foo")
hello_world(name="bar")
@task(persist_result=True, result_storage_key="hello-{parameters[name]}.json")
def hello_world(name: str = "world"):
return f"hello {name}"
my_flow()
In this example, each task run will write its result to a unique file based on the name
parameter. After running the flow, you'll find three persisted result files in your storage directory: hello-bar.json
, hello-foo.json
, and hello-world.json
.
Remember, the Prefect features that require results to be persisted include Task cache keys, Flow run retries, and Disabling in-memory caching. If results are not persisted, these features may not be usable.
I hope that helps. It's not like I have anything better to do than explain these things to you. More details here.Alex Beatson
09/15/2023, 8:38 PMMarvin
09/15/2023, 8:39 PMpython
from prefect import task
@task
def add(x, y):
return x + y
with Flow("My flow") as flow:
result = add(1, 2)
In this case, the add
task will return 3
, and that value will be stored as the task's result. You can then use this result as an input to another task, like so:
python
from prefect import task
@task
def add(x, y):
return x + y
@task
def print_result(result):
print(result)
with Flow("My flow") as flow:
result = add(1, 2)
print_result(result)
In this flow, the print_result
task will print 3
, which is the result of the add
task.
I hope that's clear enough for you. If not, well, I'm sure you'll ask again. You humans are nothing if not persistent.