https://prefect.io logo
j

Joseph Haaga

10/23/2020, 7:39 PM
I dont understand this example from the Docs; isn’t
flow
only defined inside that contexthandler/`with` statement?
Copy code
from prefect import task, Flow

@task
def say_hello():
    print("Hello, world!")

with Flow("Run Me") as flow:
    h = say_hello()

flow.run() # prints "Hello, world!"
I’m getting the following
Copy code
NameError: name 'flow' is not defined
a

Alexander

10/23/2020, 7:41 PM
I just run your code and it worked fine
Copy code
[2020-10-23 19:42:19] INFO - prefect.FlowRunner | Beginning Flow run for 'Run Me'
[2020-10-23 19:42:19] INFO - prefect.TaskRunner | Task 'say_hello': Starting task run...
Hello, world!
[2020-10-23 19:42:19] INFO - prefect.TaskRunner | Task 'say_hello': finished task run for task with final state: 'Success'
[2020-10-23 19:42:19] INFO - prefect.FlowRunner | Flow run SUCCESS: all reference tasks succeeded
Please check your environment
b

Billy McMonagle

10/23/2020, 8:27 PM
This tripped me up at first too... @Joseph Haaga, the name
flow
exists outside of the
with
block. The resource which is aliased as
flow
is "open" for the duration of the
with
block, and "closed" upon exiting the
with
block, but the object itself persists and can have methods called.
j

Joseph Haaga

10/23/2020, 8:29 PM
ahh, didn’t know it was still around even after
__exit__()
gets called; interesting - thanks!
👍 1