I have this errors. Does any one know how to fix i...
# prefect-community
w
I have this errors. Does any one know how to fix it 😭 thank you so much --------------------
Copy code
from prefect import flow

@flow
def my_favorite_function():
    print("What is your favorite number?")
    return 42

print(my_favorite_function())
------------------------ ERROR:
Copy code
Traceback (most recent call last):
  at block 1, line 8
  at /opt/python/envs/default/lib/python3.8/site-packages/prefect/flows.py, line 448, in __call__(self, return_state, wait_for, *args, **kwargs)
  at /opt/python/envs/default/lib/python3.8/site-packages/prefect/engine.py, line 164, in enter_flow_run_engine_from_flow_call(flow, parameters, wait_for, return_type)
  at /opt/python/envs/default/lib/python3.8/site-packages/anyio/_core/_eventloop.py, line 70, in run(func, backend, backend_options, *args)
  at /opt/python/envs/default/lib/python3.8/site-packages/anyio/_backends/_asyncio.py, line 292, in run(func, debug, use_uvloop, policy, *args)
  at /opt/python/lib/python3.8/asyncio/runners.py, line 33, in run(main, debug)
RuntimeError: asyncio.run() cannot be called from a running event loop
1
t
I think you should run it inside
Copy code
if __name__ == "__main__":
    print(my_favorite_function())
💯 2
w
hmm still not work
I still new to python, hope you can help me
t
Now you changed your flow to be asynchronous (
async def...
). See here: https://docs.prefect.io/tutorials/execution/#asynchronous-execution
w
But why i need to change my flow to be asynchronous? the code run fine on my local machine, but when i use a serverless IDE (Datalore), then I have that problem.
t
I don't think you have to. The initial code snipped sent by you was without async and then in the screenshot (now deleted) you used async.
w
Sorrry, I just try to fix it, and I for get to remove "async " :(((
t
I am guessing you want to reproduce this: https://docs.prefect.io/tutorials/execution/#result
w
I tried yours, but it still not work
t
Do you call it like this:
python ./your-script.py
?
w
No i don't think i did it.All the things i do is: I just opened the IDE (it looks like Jupyter note book) and try to run the code sample on from Prefect web to see how Prefect work.
Also this
b
asyncio
is a way to run things asynchronously. i'm guessing that Prefect, by default, sets up things to run asynchronously. however, you can only do that set up one time. i think your online IDE is also trying to do this asynchronous set up. i'm trying to see if there's a way for Prefect to not run it asynchronously. you said it works properly locally, yes?
w
right, it works properly on my local machine (i use Jupyter note book)
b
does it need to run in the online IDE?
and which online IDE is it?
w
yes, coz I am building an ETL pipeline and need the code to run 24/7
The online IDE is Datalore: https://datalore.jetbrains.com/
b
ah... so Prefect server is meant for running Prefect flows on some schedule and whatnot. you don't need a separate system to run it for you
w
Can you be more specific 😮
b
there's Prefect online which is a paid, managed service. and there's Prefect server, the open-source version that you need to set up and manage yourself
alternatively, if you really want to use Datalore, if you have some way to detect if it's running in Datalore, you could run
myflow.fn()
, but then you lose a lot of the advantages of Prefect
but maybe someone smarter than me has a better solution for you. i'm trying to see if there's a way to run a Prefect flow without running it in
asyncio
w
Wow, thank you. Actually, it is still hard for me to understand your comment fully. Once again, thank you 😅
b
maybe try changing
Copy code
@flow()
...
to
Copy code
from prefect.task_runners import TaskConcurrencyType

@flow(task_runner=TaskConcurrencyType.SEQUENTIAL)
see if that works?
maybe try this?
Copy code
import asyncio

asyncio.ensure_future(my_favorite_function())
w
'there's Prefect online which is a paid, managed service' => so if I use Prefect online, i need to run code on another server, and then use Prefect cloud to mange the workflow (schedule, log checking...) right?
b
right 🙂
👍 1
w
I think I would stop from here 😂 let me dive deep into the doc, maybe i need a demo from Prefect team.
👍 1
Thank Tim and Bryan for your help!
🎉 3
j
Thanks @Beege (Bryan Berry) and @Tim-Oliver. Just to clarify, Prefect Cloud (what was called Prefect online above) has a free tier. Alternatively, you can self-host the Orion server (what was called Prefect server above).
b
ahh, i didn't know about the free tier. very cool
👍 1
w
Hi Jeff Hale, thanks for you clarification! I just have one more question: If I use Prefect Cloud to schedule a workflow on my local environment, how would that happen? I mean, how Prefect Cloud can schedule a script that normally run on my local environment?
t
You need be logged into prefect cloud via api key from your local environment and then start a prefect agent which will poll for scheduled flows.
🙌 1
w
And then I just schedule the flow from Prefect Cloud?
j
Yes. 🙂
w
Yeah, thank you all. This is amazing!
🙌 2