Get help from the marimo community

Updated 2 months ago

running async inside sync cells

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