Is it possible to modify context in one task, by a...
# ask-community
v
Is it possible to modify context in one task, by adding key to the context, and get that key value in another task? What I see now, in next task context does not contain key added in previous task.
n
Hi @Vitaly Shulgin - I'd advise you against modifying the context from within your flows/tasks as it can have unexpected results; run-time context modification should happen before your flow runs. Instead, pass data between tasks as data dependencies by returning values you need from 1 task and passing them to the next.
v
got it, what would be the right approach in prefect to share some services, or external system providers, among several tasks in flow? taking into account, that at some point the flow will be scheduled in server, and I have no place to execute, or instantiate it just before the flow run
my idea was to have something like init_context task, as the first one in flow, and use it from within flow, I can not use construction like:
Copy code
with prefect.context(key='abc'):
    flow.run() # this run is successful
n
Well since the prefect context is just a prefect-provided dictionary, you could do what you're thinking with something like:
Copy code
from prefect import task, Flow

@task
def init_context():
  return dict(
    a=1,
    b=2,
    c=3
  )

with Flow("I do important things") as flow:
  context = init_context()
  
  important_thing_1(context=context, ...other_stuff_i_need)
  important_thing_2(context=context, ...other_stuff_i_need)
v
thanks, by mistake, I was trying to put a,b,c to prefect.context, instead of passing
n
No worries at all! Context looks like a good place to do that but generally it's more straightforward (and easier to track!) to provide it explicitly 🙂
v
well, but each task gets another context, if I do
prefect.context['somekey'] = value
, I can not get that key in other task
n
Sorry, I'm not sure I'm following @Vitaly Shulgin - I think the data dependency I outlined above would resolve that issue?
v
no worries, everything is fine
👍 1