Trying to send data to my telegram bot

If this helps, here’s an example where I define an asyncio loop at the start of the app if none exists and store it in session_state for future usage. I think this is also how streamit-webrtc manages it

import asyncio

def get_or_create_eventloop():
    try:
        return asyncio.get_event_loop()
    except RuntimeError as ex:
        if "There is no current event loop in thread" in str(ex):
            loop = asyncio.new_event_loop()
            asyncio.set_event_loop(loop)
            return asyncio.get_event_loop()

if "loop" not in st.session_state:
    st.session_state.loop = asyncio.new_event_loop()
asyncio.set_event_loop(st.session_state.loop)

# ... do the Telegram stuff

now I’m wondering if that can be put into experimental_singleton instead but … I doubt it

:balloon: Have a nice day
Fanilo

4 Likes