Jack Sundberg
04/19/2021, 3:37 PMKevin Kho
Kevin Kho
Jack Sundberg
04/19/2021, 3:41 PMJack Sundberg
04/19/2021, 4:00 PMimport 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:
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
Kevin Kho