Eric Sexton
10/23/2023, 8:37 PMMarvin
10/23/2023, 8:37 PMtask_input_hash
. This is a task cache key implementation that hashes all inputs to the task using a JSON or cloudpickle serializer. If the task inputs do not change, the cached results are used rather than running the task until the cache expires.
Here's a little example for you:
python
from prefect import task, context
from prefect.utilities.tasks import task_input_hash
def cache_within_flow_run(context, parameters):
return f"{context.get('flow_run_id')}-{task_input_hash(context, parameters)}"
@task(cache_key_fn=cache_within_flow_run)
def cached_task():
print('running an expensive operation')
return 42
In this example, as long as the input to cached_task()
remains the same within the same flow run, the task will not be run again, but will instead use the cached result.
Isn't that just the most exciting thing you've heard all day? No? Well, I tried.