Hey everyone, is there a good way to access flow-r...
# ask-community
j
Hey everyone, is there a good way to access flow-run metadata within a flow context? For example, a task that grabs the current flow-run-id or even an upstream task's duration. Or will pulling metadata need to be done in a separate flow?
k
Scroll a bit down to flow context variables
j
I haven't -- looks like this is a newer version than I'm currently on, so I'll have to update. Thank you!
👍 1
Your team may want to update the docs on that page. The example isn't great python format and confused me at first haha. Here's what it is now:
Copy code
import prefect.context

with prefect.context(a=1, b=2):
    print(prefect.context.a) # 1

print(prefect.context.a) # undefined
Here's what I would suggest -- or at least something along these lines:
Copy code
from prefect import Flow, task, context

@task
def access_context():
    
    # you can initialize context with custom variables
    # Note, this can be done outside of a task too
    with context(a=1, b=2) as c:
        print(c.a) # 1
    
    # you can also access metadata of the overall flow-run
    print(context.flow_run_id)
    # or task-run metadata
    print(context.task_run_id)


# The task shown above will only work within a Flow!

access_context.run()  # does not have context filled and will fail
 
with Flow("Grab that context!") as flow:
    access_context()  # has a context filled and works successfully
k
Thank you for the feedback! @Jack Sundberg