https://prefect.io logo
s

Sean Talia

01/12/2021, 5:33 PM
does anyone know if it's possible to access the labels passed to a flow during the flow's execution / within the flow context? I'd like to implement some logic that says something like:
Copy code
with Flow("test-flow") as flow:
    if "label1" in flow.labels:
        function1()
    elif "label2" in flow.labels:
        function2()
s

Spencer

01/12/2021, 5:35 PM
You'd have to put that logic in a task and utilize a filter task like
switch
j

Jim Crist-Harif

01/12/2021, 5:38 PM
If you're looking to dynamically determine the labels the flow is running under, these are available under most agents (k8s doesn't do this currently) as
prefect.config.cloud.agent.labels
. You'd need to access this inside a task and use something like a
case
block to handle branching. https://docs.prefect.io/core/idioms/conditional.html
s

Sean Talia

01/12/2021, 5:45 PM
ah interesting okay, i'm actually more interested in using the labels as a task parameter than as a mechanism for choosing which task to run; more specifically, I wanted to use a label to identify the "environment" that i'm working in, and pass this label to the flow's tasks so they know database (prod v stage) to query or which internal service to interact with
j

Jim Crist-Harif

01/12/2021, 5:48 PM
Ok. Then you'd want to access the labels at runtime via
prefect.config.agent.labels
. This should be a list of string labels. You can do whatever logic you want with it inside tasks, or pass the labels between tasks as needed (the initial access of the field just needs to be done within a task so that it happens at runtime).
Copy code
@task
def get_labels():
    return prefect.config.agent.labels
👍 1
s

Sean Talia

01/12/2021, 5:48 PM
I know this can also be done passing env variables around to my docker containers and passing those through to my flow scripts
j

Jim Crist-Harif

01/12/2021, 5:48 PM
Yeah, you have lots of options here.
s

Sean Talia

01/12/2021, 5:51 PM
okay cool that's good to know – thank you very much!
2 Views