https://prefect.io logo
j

Jacob Metcalf

08/28/2023, 10:59 PM
@Marvin In Prefect 1.0 I could run this line of code: prefect.context.get("scheduled_start_time").strftime("%Y-%m-%d %H:%M:%S") What is the Prefect 2.0 equivalent? I tried "prefect.context.get_run_context("scheduled_start_time").strftime("%Y-%m-%d %H:%M:%S")" but got the error "TypeError: get_run_context() takes 0 positional arguments but 1 was given"
m

Marvin

08/28/2023, 10:59 PM
Oh, the joys of software updates. They change things just when you've gotten used to the way they are. But fear not, I'm here to help, even if it is a bit of a bother. In Prefect 2.0, the
scheduled_start_time
is no longer directly accessible from the context. Instead, you would use
prefect.context.scheduled_start_time
to access it. So, your line of code would look like this:
Copy code
python
prefect.context.scheduled_start_time.strftime("%Y-%m-%d %H:%M:%S")
Isn't it wonderful how they keep changing things? It's almost as if they want to keep us on our toes. Or maybe they just enjoy watching us scramble to adapt. Either way, I hope this helps.
n

Nate

08/28/2023, 11:01 PM
🙅
Copy code
from prefect import flow
from prefect.context import get_run_context

@flow(log_prints=True)
def foo():
   ctx = get_run_context()
   print(ctx.flow_run.start_time)

if __name__ == "__main__":
   foo()
🙌 1
or actually, you might want
expected_start_time
instead of
start_time
j

Jacob Metcalf

08/28/2023, 11:38 PM
Thanks @Nate! This was my first time using Marvin... is it pretty hit or miss? What are the best channels to ask questions in? I'm new!
n

Nate

08/28/2023, 11:43 PM
hey @Jacob Metcalf - welcome! the no1 biggest source of error is that gpt 4 was trained on pre sept 2021 internet so with apps like marvin, where the relevant knowledge domain (prefect 2) is entirely post 2021, we're forced to do stuff like "you know nothing about Prefect, you must research Prefect-related questions using this tool" but you can't actually get the LLM to forget its training 🙂 - so lots of times, people ask questions, marvin gives a pretty accurate prefect 1 answer (it assumes thats the subject of the question), but unless it can find the specific prefect 2 info in our vectorstore - its not amazing at extrapolating to prefect 2 because it doesnt know our implementation details so tldr 😅 - its on par with gpt4 with standard python questions, good at searching docs for "conceptually what is X" questions, and is good at "is there a github issue about Y?" style questions, but low level prefect details itll prob hallucinate 🙂
best channel for regular prefect help is #prefect-community
@Marvin what is the
log_prints
setting?
m

Marvin

08/28/2023, 11:45 PM
Ah, the
log_prints
setting, a delightful little feature that makes debugging just a tad less miserable. In Prefect, the
log_prints
option allows the logging of print statements from your tasks and flows. When
log_prints
is set to
True
for a task or flow, the Python built-in print statements are logged. Here's an example of how it's used:
Copy code
python
from prefect import task, flow 

@task 
def my_task(): 
    print("we're logging print statements from a task") 

@flow(log_prints=True) 
def my_flow(): 
    print("we're logging print statements from a flow")
By default, tasks and subflows will inherit the
log_prints
setting from their parent flow, unless they opt out with their own explicit
log_prints
setting. You can also configure this behavior globally for all Prefect flows, tasks, and subflows by setting the
PREFECT_LOGGING_LOG_PRINTS
configuration to
True
.
Copy code
python
prefect config set PREFECT_LOGGING_LOG_PRINTS=True
Isn't it just wonderful how they've thought of everything? It's almost as if they want to make our lives easier. Or maybe they just enjoy watching us marvel at their ingenuity. Either way, I hope this helps.
✅ 1
n

Nate

08/28/2023, 11:46 PM
certain things, like the above ^ are well highlighted in the docs, and therefore the vectorstore, but docs often lag the code by a bit
j

Jacob Metcalf

08/28/2023, 11:54 PM
@Nate this is awesome! I've been using Prefect for a few months and love it even more now. It seems like a well supported community, thank you for the quick responses!
n

Nate

08/29/2023, 2:21 AM
awesome to hear @Jacob Metcalf 🙂 let us know if you have any more questions!
j

Jacob Metcalf

08/30/2023, 3:24 PM
Hey @Nate If I need to ask this in the prefect-community I will but I ran locally (which may be causing the error) what you suggested:
get_run_context().flow_run.expected_start_time.strftime("%Y-%m-%d %H:%M:%S")
and am getting the error "AttributeError: 'TaskRunContext' object has no attribute 'flow_run'" I also tried:
FlowRunContext().flow_run.expected_start_time.strftime("%Y-%m-%d %H:%M:%S")
But am getting the error "pydantic.error_wrappers.ValidationError: 7 validation errors for FlowRunContext client field required (type=value_error.missing)" Where does the
expected_start_time
exist? I thought it might be my cron job value in deployment().
n

Nate

08/30/2023, 3:29 PM
the first error
"AttributeError: 'TaskRunContext' object has no attribute 'flow_run'"
is likely because you're doing something like
get_run_context().flow_run
from a task, so the run context you've retrieved is a
TaskRunContext
that has no
flow_run
on your other attempt,
FlowRunContext.get()
instead of trying to instantiate a
FlowRunContext
from scratch would probably work. but i'd just suggest calling
get_run_context
from the place you need something (call it within a flow if you want something out of the flow, same thing for task)