helloheloo
11/11/2024, 8:23 AMMarvin
11/11/2024, 8:23 AMhelloheloo
11/11/2024, 8:24 AMMarvin
11/11/2024, 8:24 AMpython
from prefect import flow, task
from prefect.tasks import task_input_hash
from datetime import timedelta
# Define a task with caching enabled
@task(cache_key_fn=task_input_hash, cache_expiration=timedelta(days=1))
def my_task(x):
print(f"Running my_task with input: {x}")
return x * 2
# Define a flow that uses the caching task
@flow
def my_flow(y):
result = my_task(y)
print(f"Flow result: {result}")
# Execute the flow with a specific input
my_flow(10)
In this example, the my_task
function will only rerun if the input changes or the cache expires. This way, if you resume the flow with the same inputs, it won't redo the work unless necessary.
For more details on caching and task configurations in Prefect 3.0, you can check out the official documentation here. If you need a reminder why it feels like you're already at yesterday's party when flows don't resume correctly, just remember Marvin... he wouldn't have cached anything either, but you can!