Get help from the marimo community

Updated 4 months ago

running async inside sync cells

At a glance

The community member has a long chained function call that needs to call some async function to fetch data from a database. They tried using asyncio to get the running loop and run the function inside that loop using "run_until_complete", but encountered a "RuntimeError: this event loop is already running" error. The community members discussed using the "nest_asyncio" library as a potential solution, though one community member found it to be a "hacky" approach. Another community member suggested that the function could be called from a callback triggered by a user click, but noted that async callbacks may not be possible, so the function would need to be made synchronous. There was no explicitly marked answer in the comments.

I have a long chained function call, which needs to call some async function to fetch data from a database. I was thinking I could use asyncio to get the running loop, and then run the function inside that loop using "run_until_complete", but I just get an runtimeerror "this event loop is already running". How can I handle this without having to rewrite all my functions to async instead?
M
j
W
6 comments
I am not sure what hackery this library does and if there is anything we can learn from it, but i was able to use nest_asyncio
Plain Text
import asyncio
import nest_asyncio
nest_asyncio.apply()

async def run_thing():
    await asyncio.sleep(2)

asyncio.get_running_loop().run_until_complete(run_thing())
Thanks, I did read about that, but it seems very hacky indeed πŸ™‚
the function I need to call is inside a callback that is triggered when the user clicks a button. but I cant use async callbacks can I, so I have to make it sync ..
got it. yea we could maybe extend on_click to allow async. im not sure if there are any limitations for us to do that
This worked for me as well, though more straightforward behavior would be welcome πŸ˜‰
Add a reply
Sign up and join the conversation on Discord