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.
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/"}']
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/"}']
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/"}
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
#
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:
https://github.com/ipython/ipykernel/pull/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:
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"
Hi Lucas,
If you're using asynchronous functionality, I would suggest using another
IDE. I have switched to Pycharm and have had no issues since. You will not
currently be able to use Spyder with asynchronous functions.
Ryan
On Tue, Aug 28, 2018, 5:56 PM lucasgriff88 notifications@github.com wrote:
I am trying to learn how to use asyncio for asynchronous data acquisition.
I found some example code from a tutorial:
[image: 180828_spyder_error]
https://user-images.githubusercontent.com/17008269/44734788-59103300-aab0-11e8-8239-985ef31a0780.PNGI 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"—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
https://github.com/spyder-ide/spyder/issues/7096#issuecomment-416639765,
or mute the thread
https://github.com/notifications/unsubscribe-auth/AIopJUw28cmIo_5jyvW6Ga5cmohEMHalks5uVWgYgaJpZM4T5Eo-
.
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?
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:
Is that considered a permanent solution or a quick fix?
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
https://github.com/spyder-ide/spyder/issues/7096?email_source=notifications&email_token=ACFCSJLQISX2DP4WJXX7ROLQCKXYJA5CNFSM4E7EJI7KYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOD3KBSGQ#issuecomment-517216538,
or mute the thread
https://github.com/notifications/unsubscribe-auth/ACFCSJONXWHBHNMZT36Z7SLQCKXYJANCNFSM4E7EJI7A
.
@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 https://github.com/spyder-ide/spyder-kernels/pull/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.
Most helpful comment
I got the issue resolved by using the nest_async
pip install nest_asyncio
and adding below lines in my file.