https://prefect.io logo
v

Vitaly Shulgin

12/18/2020, 2:51 PM
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

nicholas

12/18/2020, 2:58 PM
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

Vitaly Shulgin

12/18/2020, 4:39 PM
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

nicholas

12/18/2020, 4:45 PM
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

Vitaly Shulgin

12/18/2020, 4:47 PM
thanks, by mistake, I was trying to put a,b,c to prefect.context, instead of passing
n

nicholas

12/18/2020, 4:51 PM
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

Vitaly Shulgin

12/18/2020, 5:05 PM
well, but each task gets another context, if I do
prefect.context['somekey'] = value
, I can not get that key in other task
n

nicholas

12/18/2020, 6:06 PM
Sorry, I'm not sure I'm following @Vitaly Shulgin - I think the data dependency I outlined above would resolve that issue?
v

Vitaly Shulgin

12/18/2020, 7:12 PM
no worries, everything is fine
👍 1