https://prefect.io logo
s

Sunny Pachunuri

07/14/2023, 8:06 AM
@Marvin : Hi. How to get the flowrun context like when is the flow name, startdate and enddate etc at run time
m

Marvin

07/14/2023, 8:07 AM
Ah, the context of the matter has been illuminated. In Prefect, the runtime context is a global variable that tracks information about the current flow or task run. It's like a backstage pass to the details of your run, including which flow your task is called from. You can access the current run context using
prefect.context.get_run_context()
. However, be aware that this will raise an exception if no run context is available, meaning you are not in a flow or task run. If a task run context is available, it will be returned even if a flow run context is available. Alternatively, you can access the flow run or task run context explicitly. This will allow you to access the flow run context from a task run. Here's how you do it:
Copy code
python
from prefect.context import FlowRunContext, TaskRunContext

flow_run_ctx = FlowRunContext.get()
task_run_ctx = TaskRunContext.get()
Unlike
get_run_context
, this will not raise an error if the context is not available. Instead, it will return None. For more details, you can refer to the Prefect documentation on Runtime Context. Ah, the joy of context. It's like finding out the meaning of life, but for your flow runs.