Are you able to cache results from workflows, to b...
# ask-community
a
Are you able to cache results from workflows, to be reused on a new flow run. When I tested within the same workflow tasks were successfully cached but when I tried
Copy code
prefect config set PREFECT_RESULTS_PERSIST_BY_DEFAULT=true
on independent flow runs triggered by
Copy code
flow_run = await run_deployment("embed-pdf/default", parameters={"url": "test/sample.pdf"}, timeout=0)
i'm not seeing any cached results
1
c
Hey Anthony - did you run that
config set
command locally and then trigger the deployment? If it runs remotely then your local profile will have no bearing on its result persistence. To configure settings on remote deployments you have a few options: • environment variables (maybe the easiest to set) • setting files (
.env
or
prefect.toml
or
pyproject.toml
): these have the benefit of being source controlled next to your code For more information check out the settings documentation here: https://docs.prefect.io/3.0/develop/settings-and-profiles#configure-settings-and-profiles
a
Here is my work flow process that happens locally In the terminal 1 i'm running
Copy code
prefect config set PREFECT_RESULTS_PERSIST_BY_DEFAULT=true
Set 'PREFECT_RESULTS_PERSIST_BY_DEFAULT' to 'true'.
Updated profile 'local'.
then
worker.py
Copy code
# worker.py
from core.workers.workflows.embed_pdf import embed_pdf


def deploy():
    embed_pdf.serve(
        name="default",
        version="1.0.0",
    )
then in a separate terminal i'm proceeding to trigger the worker via
run_deployment(..)
I also run the
I also have
@task(_log_prints_=True, _persist_result_=True)
for each task
c
I’ll need more information about the problem you’re seeing to help
a
It may be an env issue on my end because when I set
@task(_log_prints_=True, _cache_policy_=TASK_SOURCE)
the caching does work. Thank you!
🫡 1
c
ah gotcha, yea if you're seeing issues with when tasks are cached or rerun this doc may help: https://docs.prefect.io/3.0/develop/task-caching
a
One thing I want to confirm is I only have to set the env within the
workflow
process, or do I also have to set it in the process where i run
prefect server start
c
for anything related to task/flow results or task/flow caching, you just need to set the environment variables / setting in the workflow process. All the settings that affect the server are prefixed with
PREFECT_SERVER_
👍
🙌 1
a
Oh I think I see the issue, when, the caching worked previously, my inputs/source and flow run id was the same since it was a long running job and that was with the DEFAULT setting. https://docs.prefect.io/3.0/develop/task-caching#cache-policies Is there a way to cache based on tasks inputs and code definition (but not specific to a flow run id)?
Copy code
@task(cache_policy=TASK_SOURCE + INPUTS)
c
Gotcha yea, by default we try to keep caching scope useful but limited to individual workflow executions to avoid surprises. Yup there certainly is! All cache policies can be combined / customized. Yup you got it!
haha no worries, glad you figured it out