Hi community, is there a way to change the flow ru...
# ask-community
d
Hi community, is there a way to change the flow run context during a task run, so that the subsequent tasks can see the updated context values?
a
@Dan Zhao can you describe the use case for which you would want to use it? In general, mutating the context from within a task is not recommended as it will not behave consistently across execution environments. But you can do that using the context manager:
Copy code
import prefect

with prefect.context(dict(a=1, b=2)):
    print(prefect.context.a) # 1

print(prefect.context.get("a")) # undefined
More on that: • https://docs.prefect.io/api/latest/utilities/context.htmlhttps://docs.prefect.io/core/concepts/execution.html#prefect-supplied-context If you are on Cloud, you can use the KV Store instead of context to store a small piece of information that you can access across tasks: https://docs.prefect.io/orchestration/concepts/kv_store.html#kv-store
d
I am hoping to alter some system status (basically environment variables, but context sounds like a more robust option) from within a task's run method. Subsequent tasks should be able to see these changes.
a
if those variables are sensitive, you could leverage PrefectSecret, otherwise you can also set environment variables within your run configuration - those will be then applied on a flow-level.
Copy code
flow.run_config = UniversalRun(env={"SOME_VAR": "value"})
d
for the run configuration approach - is it possible to change it during a task run, while allowing subsequent tasks to see it?
a
For that I would rather use KV Store. I mean, you can of course do this in your task:
Copy code
import os
os.environ["YOUR_ENV_VARIABLE"] = "VALUE"
but it’s not as reliable as setting it with KV Store:
Copy code
from prefect.backend import set_key_value

key_value_uuid = set_key_value(key="YOUR_ENV_VARIABLE", value="VALUE")
upvote 1