RuntimeError: There is no current event loop in thread 'ScriptRunner.scriptThread'

Hi guys,
Have not seen a thread error in the forum - hope someone can help!

I am using Eikon Data API with Streamlit and get the following error - at the import stage itself:

File "c:\users\xxx\anaconda3\lib\site-packages\streamlit\ScriptRunner.py", line 322, in _run_script
    exec(code, module.__dict__)File "C:\Users\xxx\st.py", line 17, in <module>
    import eikon as ekFile "c:\users\xxx\anaconda3\lib\site-packages\eikon\__init__.py", line 12, in <module>
    from .Profile import *File "c:\users\xxx\anaconda3\lib\site-packages\eikon\Profile.py", line 22, in <module>
    from .streaming_session import DesktopSessionFile "c:\users\xxx\anaconda3\lib\site-packages\eikon\streaming_session\__init__.py", line 3, in <module>
    from .session import *File "c:\users\xxx\anaconda3\lib\site-packages\eikon\streaming_session\session.py", line 21, in <module>
    nest_asyncio.apply()File "c:\users\xxx\anaconda3\lib\site-packages\nest_asyncio.py", line 9, in apply
    loop = loop or asyncio.get_event_loop()File "c:\users\xxx\anaconda3\lib\asyncio\events.py", line 644, in get_event_loop
    % threading.current_thread().name)

Donโ€™t get the error when I dont import eikon module. Conversely, dont get the error when I use eikon module without streamlit!

Any pointers on why this is happening?

Thanks

2 Likes
    import asyncio
    import aiohttp

    async def main():
        async with aiohttp.ClientSession() as session:
            async with session.get('http://httpbin.org/get') as resp:
                print(resp.status)
                print(await resp.text())

    loop = asyncio.get_event_loop()
    loop.run_until_complete(main())

Iโ€™m using this in the streamlit app and am getting the same error. :confused:

Hi guys, any solution to this? Thanks

python - RuntimeError: There is no current event loop in thread in async + apscheduler - Stack Overflow

replace:
loop = asyncio.get_event_loop()
by:
loop = asyncio.new_event_loop()
asyncio.set_event_loop(loop)

Hi, I get this same error.

Only when importing eikon and streamlit together.

Is there a workaround?

@ib2020 if you set up your coroutine and then execute it with:

# create a coro function `coro` up here

loop = new_event_loop()
set_event_loop(loop)
results = run(coro)

it will execute.

Hi Bill,
Am not very familiar with coro functions. Can you pls expand on your solution a little bit?

Many thanks
BC

@Hepbc thatโ€™s just an arbitrary name of the object which should be understood as a coroutine, as theyโ€™re created via tools like asyncio, futures, etcโ€ฆ