New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
baumga34 opened this Issue May 9, 2018 · 10 comments

Comments

Projects
None yet
5 participants
@baumga34
Copy link

baumga34 commented May 9, 2018

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)

@baumga34

This comment has been minimized.

Copy link
Author

baumga34 commented May 9, 2018

I am not using conda or anaconda.

@ccordoba12 ccordoba12 added this to the v3.x milestone May 10, 2018

@ccordoba12

This comment has been minimized.

Copy link
Member

ccordoba12 commented May 10, 2018

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.

@pgeorgan

This comment has been minimized.

Copy link

pgeorgan commented Jun 14, 2018

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.

@ccordoba12

This comment has been minimized.

Copy link
Member

ccordoba12 commented Jun 14, 2018

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.

@pgeorgan

This comment has been minimized.

Copy link

pgeorgan commented Jun 14, 2018

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?

@pgeorgan

This comment has been minimized.

Copy link

pgeorgan commented Jun 14, 2018

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

@lucasgriff88

This comment has been minimized.

Copy link

lucasgriff88 commented Aug 28, 2018

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"

@baumga34

This comment has been minimized.

Copy link
Author

baumga34 commented Aug 28, 2018

@ccordoba12 ccordoba12 removed this from the v3.x milestone Oct 11, 2018

@ccordoba12

This comment has been minimized.

Copy link
Member

ccordoba12 commented Oct 11, 2018

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

@ccordoba12 ccordoba12 closed this Oct 11, 2018

@RajshekarReddy

This comment has been minimized.

Copy link

RajshekarReddy commented Dec 23, 2018

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()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment