https://prefect.io logo
Title
w

Will Truong

01/04/2023, 9:49 AM
I have this errors. Does any one know how to fix it 😭 thank you so much --------------------
from prefect import flow

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

print(my_favorite_function())
------------------------ ERROR:
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

Tim-Oliver

01/04/2023, 9:54 AM
I think you should run it inside
if __name__ == "__main__":
    print(my_favorite_function())
💯 2
w

Will Truong

01/04/2023, 9:55 AM
hmm still not work
I still new to python, hope you can help me
t

Tim-Oliver

01/04/2023, 9:57 AM
Now you changed your flow to be asynchronous (
async def...
). See here: https://docs.prefect.io/tutorials/execution/#asynchronous-execution
w

Will Truong

01/04/2023, 10:01 AM
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

Tim-Oliver

01/04/2023, 10:05 AM
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

Will Truong

01/04/2023, 10:06 AM
Sorrry, I just try to fix it, and I for get to remove "async " :(((
t

Tim-Oliver

01/04/2023, 10:06 AM
I am guessing you want to reproduce this: https://docs.prefect.io/tutorials/execution/#result
w

Will Truong

01/04/2023, 10:06 AM
I tried yours, but it still not work
t

Tim-Oliver

01/04/2023, 10:07 AM
Do you call it like this:
python ./your-script.py
?
w

Will Truong

01/04/2023, 10:10 AM
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

Beege (Bryan Berry)

01/04/2023, 10:18 AM
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

Will Truong

01/04/2023, 10:19 AM
right, it works properly on my local machine (i use Jupyter note book)
b

Beege (Bryan Berry)

01/04/2023, 10:20 AM
does it need to run in the online IDE?
and which online IDE is it?
w

Will Truong

01/04/2023, 10:21 AM
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

Beege (Bryan Berry)

01/04/2023, 10:22 AM
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

Will Truong

01/04/2023, 10:23 AM
Can you be more specific 😮
b

Beege (Bryan Berry)

01/04/2023, 10:24 AM
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

Will Truong

01/04/2023, 10:31 AM
Wow, thank you. Actually, it is still hard for me to understand your comment fully. Once again, thank you 😅
b

Beege (Bryan Berry)

01/04/2023, 10:35 AM
maybe try changing
@flow()
...
to
from prefect.task_runners import TaskConcurrencyType

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

asyncio.ensure_future(my_favorite_function())
w

Will Truong

01/04/2023, 10:38 AM
'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

Beege (Bryan Berry)

01/04/2023, 10:38 AM
right 🙂
👍 1
w

Will Truong

01/04/2023, 10:40 AM
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

Jeff Hale

01/04/2023, 1:49 PM
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

Beege (Bryan Berry)

01/05/2023, 2:05 AM
ahh, i didn't know about the free tier. very cool
👍 1
w

Will Truong

01/10/2023, 9:54 AM
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

Tim-Oliver

01/10/2023, 9:56 AM
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

Will Truong

01/10/2023, 10:02 AM
And then I just schedule the flow from Prefect Cloud?
j

Jeff Hale

01/10/2023, 1:07 PM
Yes. 🙂
w

Will Truong

01/10/2023, 1:47 PM
Yeah, thank you all. This is amazing!
🙌 2