“RuntimeError: This event loop is already running”; debugging aiohttp, asyncio and IDE “spyder3” in python 3.6.5
See original GitHub issueProblem 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)
Issue Analytics
- State:
- Created 5 years ago
- Comments:18 (5 by maintainers)

Top Related StackOverflow Question
I got the issue resolved by using the nest_async
pip install nest_asyncioand adding below lines in my file.
The solution is to not use spyder. The code works in pycharm, which is what I’ve switched to.
On Thu, Aug 1, 2019, 4:59 AM EEflow notifications@github.com wrote: