<@ULVA73B9P> Is there a way to put a value in the ...
# ask-marvin
r
@Marvin Is there a way to put a value in the context in the main flow and then accessing it from any sub-flows or tasks?
m
To put a value in the Prefect context in the main flow and then access it from any sub-flows or tasks, follow this approach: 1. Setting the context in the main flow: You can manually update the
context
during the main flow run before any sub-flows or tasks are run. Use the
prefect.context
namespace to set your value.
Copy code
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
.
Copy code
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.
r
Is this prefect 1?