ib_insync icon indicating copy to clipboard operation
ib_insync copied to clipboard

Error using ib_insync with Streamlit.io - 'There is no current event loop'

Open d416 opened this issue 3 years ago • 2 comments

There is a whole thread of people trying to get ib_insync to work within Streamlit without success. Error is 'There is no current event loop.' https://github.com/streamlit/streamlit/issues/744

Maybe we can get a code recipe for doing this in the ib_insync docs? (hopefully it's this simple)

d416 avatar Mar 04 '23 14:03 d416

Add this code before import ib_insync:

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()

loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)

Anton-Filimoncev avatar Mar 25 '23 17:03 Anton-Filimoncev

This is not specific to ib_insync but to any asyncio application.

erdewit avatar Apr 02 '23 10:04 erdewit