Runar
01/24/2024, 10:12 AMMarvin
01/24/2024, 10:12 AMcontext
during the main flow run before any sub-flows or tasks are run. Use the prefect.context
namespace to set your value.
python
from prefect import context
# Assuming you are inside the main flow definition
context.update(my_custom_key='my_custom_value')
2. Accessing values from the context: In both sub-flows and tasks, you can access this value using prefect.context
.
python
from prefect import task, Flow
from prefect.context import get_run_context
@task
def my_task():
# Accessing context within a task
run_context = get_run_context()
my_value = run_context.get('my_custom_key')
# Alternatively, in a task you can directly use
# my_value = prefect.context.get('my_custom_key')
print(my_value)
with Flow('my-sub-flow') as sub_flow:
# Accessing context within a sub-flow
run_context = get_run_context()
my_value = run_context.get('my_custom_key')
print(my_value)
my_task()
# Make sure to call this sub-flow as part of the main flow for the context value to propagate
Please note that passing context to distributed task workers may not be optimal because the context can be costly to serialize and deserialize, so this approach assumes you are not dealing with distributed task workers. If you are in a context that does not have a run context (i.e., outside a flow or task run), trying to access it will raise an exception.Runar
01/24/2024, 10:13 AM