Thread
#prefect-community
    v

    Vitaly Shulgin

    1 year ago
    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.
    nicholas

    nicholas

    1 year ago
    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

    1 year ago
    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:
    with prefect.context(key='abc'):
        flow.run() # this run is successful
    nicholas

    nicholas

    1 year ago
    Well since the prefect context is just a prefect-provided dictionary, you could do what you're thinking with something like:
    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

    1 year ago
    thanks, by mistake, I was trying to put a,b,c to prefect.context, instead of passing
    nicholas

    nicholas

    1 year ago
    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

    1 year ago
    well, but each task gets another context, if I do
    prefect.context['somekey'] = value
    , I can not get that key in other task
    nicholas

    nicholas

    1 year ago
    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

    1 year ago
    no worries, everything is fine