<@ULVA73B9P> I want to access a datetime value at ...
# ask-marvin
a
@Marvin I want to access a datetime value at each the flow was scheduled from flow code. How can I extract this value from prefect flow context?
m
You can access various runtime contexts in Prefect using the
prefect.context.get_run_context()
function. This function can help you retrieve the context of the flow run and potentially the datetime it was scheduled. Unfortunately, the exact answer for accessing the scheduled datetime is not directly found in the given excerpts. For more detailed information on accessing the flow context and retrieving specific runtime information such as the scheduled datetime, please refer to the Prefect documentation on runtime context here. The guide on schedules might also offer some clues, but you might need to dig deeper for specifics regarding the scheduled datetime here. If you face an exception with
get_run_context()
, it means you are not in a flow or task run, and if you want to access the flow run context specifically, even from within a task, you should explicitly access the flow run context. For example:
Copy code
python
from prefect import context

flow_run_context = context.get_run_context().flow_run
# Access specific attributes of the flow run context here
Please note that not all desired metadata might always be available directly in the context, depending on the execution environment and how Prefect is configured.