spyder-ide/spyder

“RuntimeError: This event loop is already running”; debugging aiohttp, asyncio and IDE “spyder3” in python 3.6.5

baumga34 opened this issue · 18 comments

Problem Description

https://stackoverflow.com/questions/50243393/runtimeerror-this-event-loop-is-already-running-debugging-aiohttp-asyncio-a
From the above link
I'm struggling to understand why I am getting the "RuntimeError: This event loop is already running" runtime error. I have tried to run snippets of code from "https://aiohttp.readthedocs.io/en/stable/" however, I keep getting the same issue.

What steps reproduce the problem?

1. Run the following code in Spyder IDE


import aiohttp
import asyncio
import time

urls = ['https://api.robinhood.com/quotes/c4f6720a-0364-4e7b-8c41-705696759e1a/']

async def fetch(client, url):
    async with client.request('get', url) as response:
        if response.status == 200:
            data = await response.text()
        else:
            data = []
        print(data)
        return(data)

async def get_async_urls(urls):
    async with aiohttp.ClientSession() as client:
        return await asyncio.gather(*(fetch(client, url) for url in urls))

if __name__ == '__main__':
    t0 = time.time()
    loop = asyncio.get_event_loop()
    results = loop.run_until_complete(get_async_urls(urls))
    print(results)
    t1 = time.time()
    total_time = t1-t0
    loop.close()

Results:
RuntimeError: This event loop is already running
{"ask_price":"14.9000","ask_size":100,"bid_price":"14.0100","bid_size":100,"last_trade_price":"14.7900","last_extended_hours_trade_price":"14.7900","previous_close":"14.3600","adjusted_previous_close":"14.3600","previous_close_date":"2018-05-07","symbol":"SURF","trading_halted":false,"has_traded":true,"last_trade_price_source":"consolidated","updated_at":"2018-05-08T20:01:21Z","instrument":"https://api.robinhood.com/instruments/43d56335-f2f6-4711-b650-55be2396f814/"}
Expected Results:
{"ask_price":"14.9000","ask_size":100,"bid_price":"14.0100","bid_size":100,"last_trade_price":"14.7900","last_extended_hours_trade_price":"14.7900","previous_close":"14.3600","adjusted_previous_close":"14.3600","previous_close_date":"2018-05-07","symbol":"SURF","trading_halted":false,"has_traded":true,"last_trade_price_source":"consolidated","updated_at":"2018-05-08T20:01:21Z","instrument":"https://api.robinhood.com/instruments/43d56335-f2f6-4711-b650-55be2396f814/"} ['{"ask_price":"14.9000","ask_size":100,"bid_price":"14.0100","bid_size":100,"last_trade_price":"14.7900","last_extended_hours_trade_price":"14.7900","previous_close":"14.3600","adjusted_previous_close":"14.3600","previous_close_date":"2018-05-07","symbol":"SURF","trading_halted":false,"has_traded":true,"last_trade_price_source":"consolidated","updated_at":"2018-05-08T20:01:21Z","instrument":"https://api.robinhood.com/instruments/43d56335-f2f6-4711-b650-55be2396f814/"}']

2. Run the code from above with cmd.exe

Results:
{"ask_price":"14.9000","ask_size":100,"bid_price":"14.0100","bid_size":100,"last_trade_price":"14.7900","last_extended_hours_trade_price":"14.7900","previous_close":"14.3600","adjusted_previous_close":"14.3600","previous_close_date":"2018-05-07","symbol":"SURF","trading_halted":false,"has_traded":true,"last_trade_price_source":"consolidated","updated_at":"2018-05-08T20:01:21Z","instrument":"https://api.robinhood.com/instruments/43d56335-f2f6-4711-b650-55be2396f814/"} ['{"ask_price":"14.9000","ask_size":100,"bid_price":"14.0100","bid_size":100,"last_trade_price":"14.7900","last_extended_hours_trade_price":"14.7900","previous_close":"14.3600","adjusted_previous_close":"14.3600","previous_close_date":"2018-05-07","symbol":"SURF","trading_halted":false,"has_traded":true,"last_trade_price_source":"consolidated","updated_at":"2018-05-08T20:01:21Z","instrument":"https://api.robinhood.com/instruments/43d56335-f2f6-4711-b650-55be2396f814/"}']

3. The above results match what I expect.

What is the expected output? What do you see instead?

Expected Results:
{"ask_price":"14.9000","ask_size":100,"bid_price":"14.0100","bid_size":100,"last_trade_price":"14.7900","last_extended_hours_trade_price":"14.7900","previous_close":"14.3600","adjusted_previous_close":"14.3600","previous_close_date":"2018-05-07","symbol":"SURF","trading_halted":false,"has_traded":true,"last_trade_price_source":"consolidated","updated_at":"2018-05-08T20:01:21Z","instrument":"https://api.robinhood.com/instruments/43d56335-f2f6-4711-b650-55be2396f814/"} ['{"ask_price":"14.9000","ask_size":100,"bid_price":"14.0100","bid_size":100,"last_trade_price":"14.7900","last_extended_hours_trade_price":"14.7900","previous_close":"14.3600","adjusted_previous_close":"14.3600","previous_close_date":"2018-05-07","symbol":"SURF","trading_halted":false,"has_traded":true,"last_trade_price_source":"consolidated","updated_at":"2018-05-08T20:01:21Z","instrument":"https://api.robinhood.com/instruments/43d56335-f2f6-4711-b650-55be2396f814/"}']
Results:
RuntimeError: This event loop is already running
{"ask_price":"14.9000","ask_size":100,"bid_price":"14.0100","bid_size":100,"last_trade_price":"14.7900","last_extended_hours_trade_price":"14.7900","previous_close":"14.3600","adjusted_previous_close":"14.3600","previous_close_date":"2018-05-07","symbol":"SURF","trading_halted":false,"has_traded":true,"last_trade_price_source":"consolidated","updated_at":"2018-05-08T20:01:21Z","instrument":"https://api.robinhood.com/instruments/43d56335-f2f6-4711-b650-55be2396f814/"}

Paste Traceback/Error Below (if applicable)

runfile('C:/Playground/Github/tradey/app/test_fast_web_00002.py', wdir='C:/Playground/Github/tradey/app')
Traceback (most recent call last):

  File "<ipython-input-2-437c9e79feb0>", line 1, in <module>
    runfile('C:/Playground/Github/tradey/app/test_fast_web_00002.py', wdir='C:/Playground/Github/tradey/app')

  File "c:\playground\python\python36-32\lib\site-packages\spyder\utils\site\sitecustomize.py", line 705, in runfile
    execfile(filename, namespace)

  File "c:\playground\python\python36-32\lib\site-packages\spyder\utils\site\sitecustomize.py", line 102, in execfile
    exec(compile(f.read(), filename, 'exec'), namespace)

  File "C:/Playground/Github/tradey/app/test_fast_web_00002.py", line 23, in <module>
    loop.run_until_complete(main())

  File "c:\playground\python\python36-32\lib\asyncio\base_events.py", line 455, in run_until_complete
    self.run_forever()

  File "c:\playground\python\python36-32\lib\asyncio\base_events.py", line 409, in run_forever
    raise RuntimeError('This event loop is already running')

RuntimeError: This event loop is already running

Versions

  • Spyder version: 3.2.8
  • Python version: 3.6.5
  • Qt version: 5.9.3
  • PyQt version: 5.9.2
  • Operating System name/version: Windows

# Dependencies

IPython >=4.0    :  6.3.1 (OK)
cython >=0.21    :  0.28.2 (OK)
jedi >=0.9.0     :  0.12.0 (OK)
nbconvert >=4.0  :  5.3.1 (OK)
numpy >=1.7      :  1.14.2 (OK)
pandas >=0.13.1  :  0.22.0 (OK)
pycodestyle >=2.3:  2.4.0 (OK)
pyflakes >=0.6.0 :  1.6.0 (OK)
pygments >=2.0   :  2.2.0 (OK)
pylint >=0.25    :  1.8.4 (OK)
qtconsole >=4.2.0:  4.3.1 (OK)
rope >=0.9.4     :  0.10.7 (OK)
sphinx >=0.6.6   :  1.7.2 (OK)
sympy >=0.7.3    :  1.1.1 (OK)

I am not using conda or anaconda.

I really don't know what happens in this case, sorry. We'll try to take a look at it in the future, but I can't make promises.

In the meantime, you can update IPython, tornado, jupyter_client and pyzmq to see if that solves this problem.

I'm having this same issue. Simply opening Spyder (latest version) spawns multiple Python 3.6 processes for me. Was not an issue until today, when I updated conda and it's packages (--all). Code itself works fine if executed from Mac Terminal command line.

I think this happens because IPython and/or ipykernel doesn't have support for the asyncio event loop:

ipython/ipykernel#323

So I think it's not something we can fix in Spyder.

Hmm. It worked with IPython 5.3.0 just fine.

Disregard the comment about spawning python processes. It appears this is a feature of code-completion features in the Editor (as per your own answer on StackOverflow I just came across).

Neverthless, why would it work with a previous version?

This worked for me "Can't invoke asyncio event_loop after tornado 5.0 update". Idk why, maybe just lucky.

I am trying to learn how to use asyncio for asynchronous data acquisition. I found some example code from a tutorial:
180828_spyder_error

I found that loop.close() did not ever run. A print command after loop.run_until_complete did not print. Lastly loop.stop() results in "Kernel died, restarting"

This should be fixed now with the latest IPython and ipykernel versions.

I got the issue resolved by using the nest_async

pip install nest_asyncio

and adding below lines in my file.

import nest_asyncio
nest_asyncio.apply()

Any updates? This simple script is still giving an error using Spyder:

import asyncio

async def main():
    print(1)

asyncio.run(main())

Use nest_asyncio, as mentioned above by @RajshekarReddy.

Is that considered a permanent solution or a quick fix?

@EEflow, you're welcome to submit a PR to spyder-kernels to add support for nest_asyncio, which would be similar to what we did for wurlitzer in spyder-ide/spyder-kernels#54. For now, the workaround is to activate net_asyncio manually.

@baumga34, your comments are simply not constructive (you already did it once, so I don't understand why you're repeating it again). If you don't have anything substantial to add about Spyder, please refrain from doing the same kind of comments in the future.

***Edit: I'm sorry, I got here from a google search and assumed it was for the asyncio package. I now realize it is for spyder so I'm definitely in the wrong place. Please ignore the rest of my message. I'll leave it below the line for completeness of records.


import nest_asyncio
nest_asyncio.apply()

The above code seems to solve most problems but it still crashes the python kernel when I call
loop.stop() from within Jupyter Notebooks. Presumably because of the same underlying reason as it does for Spyder.

Does anyone have any thoughts, solutions or workarounds?

Thanks in advance.

p.s. please let me know if this isn't the right place for this comment, I'm relatively new here.

This is still not fixed (aiohttp 3.7.4 + python 3.8.8)

Any fix for this problem??