https://prefect.io logo
Title
p

Philip MacMenamin

08/26/2022, 5:05 PM
Hi, I'm wondering if there's a way to attach a key to a
context
value on a per run basis?
👀 1
1
m

Mason Menges

08/26/2022, 7:13 PM
Hey @Philip MacMenamin I'm having some trouble conceptualizing what you're trying to do here haha😅, Do you mind describing what you're trying to accomplish here?
p

Philip MacMenamin

08/26/2022, 7:20 PM
Hi Mason - sorry to be unclear, something along the lines of:
@task
def my_task_2(i: int) -> Tuple[int, int]:
# read something from context
fp = context.fp
do_thing(fp)
return (1, 2)
@task
def my_task() -> Tuple[int, int]:
# assign something to context
prefect.context.fp="/tmp/blah"
return (1, 2)
with Flow("flow_run_name") as f:
a, b = my_task()
my_task_2(a)
I'd like a mechanism to set something (using something that derives from a
Parameter
that's passed into the flow on a per run basis) in an initial task of a workflow that will downstream tasks to get that value, if needed.
m

Mason Menges

08/26/2022, 7:36 PM
So something like this would probably work, the config is technically just a dictionary that gets reference when the flow is run so this should only create a key value pair in the dictionary on a per run basis that you can reference in other flows, I'm not 100% certain this would work well in a flow context. That being said it might make more sense to create a separate task to generate the appropriate parameter based on the current flow run rather than setting it in the config context
from prefect import Flow, task, config

@task
def foo(x: int):
    config["test_key"] = "somval"


@task
def bar():
    print(config["test_key"])

with Flow('test-config') as flow:
    foo()
    bar()

flow.run()
👍 1
p

Philip MacMenamin

08/26/2022, 7:40 PM
Ah,
config
. OK, yup, this does what I want. Thank you.
m

Mason Menges

08/26/2022, 7:41 PM
No problem 😄