10

This seems like a common problem, see for example: RuntimeError: This event loop is already running in python

But in my case, I'm only starting the event loop once, at least as far as I can see. Also this example follows directly the instructions here:

import asyncio

loop = asyncio.get_event_loop()

async def coroutine():
    print("hey")
    await asyncio.sleep(1)
    print("ho")
    return 1


async def main():

    tasks = []
    for i in range(2):
        tasks.append(asyncio.ensure_future(coroutine()))
    await asyncio.gather(*tasks)

results = loop.run_until_complete(main())
loop.close()

This prints an error message, and the output of the print() calls in the coroutines:

---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-1-f4a74fbfac46> in <module>
     16     await asyncio.gather(*tasks)
     17 
---> 18 results = loop.run_until_complete(asyncio.gather(*tasks))
     19 loop.close()

~/anaconda3/envs/keras_dev/lib/python3.6/asyncio/base_events.py in run_until_complete(self, future)
    453         future.add_done_callback(_run_until_complete_cb)
    454         try:
--> 455             self.run_forever()
    456         except:
    457             if new_task and future.done() and not future.cancelled():

~/anaconda3/envs/keras_dev/lib/python3.6/asyncio/base_events.py in run_forever(self)
    407         self._check_closed()
    408         if self.is_running():
--> 409             raise RuntimeError('This event loop is already running')
    410         if events._get_running_loop() is not None:
    411             raise RuntimeError(

RuntimeError: This event loop is already running
hey
hey
ho
ho

And the results variable stays undefined.

How can I spin up a list of coroutines and gather their outputs correctly ?

lhk
  • 27,458
  • 30
  • 122
  • 201
  • 1
    Note that ``get_event_loop`` will not get you a new loop but the one assigned to the current thread. Since your traceback indicates you are using an interactive session, it is likely you already used it by accident. Also, if you do not care about ``asyncio`` in specific, I suggest taking a look at ``trio`` or ``curio``. – MisterMiyagi Nov 11 '18 at 11:53
  • That's what I thought, too (this is jupyter :P) but I restarted the kernel before running this. – lhk Nov 11 '18 at 13:20

1 Answers1

11

I also came across this problem after doing some upgrades. It turns out that the tornado package is most likely the culprit. If you have tornado>=5.0 then running event loops in a notebook causes conflicts. There's a detailed discussion here but the solution, for now, is just to downgrade with pip install tornado==4.5.3.

kayoz
  • 1,104
  • 12
  • 16
  • 1
    I'm having this problem and this solution helped but this is quite unfortunate because I'm using `asyncio` in Jupyter Notebook (jupyterhub 1.1.0) and it requires tornado>=5.0. – Anna Feb 21 '20 at 21:04
  • i install less version of tornado==4.5.3, when i open jupyter notebook it shows nothing (existing files and folders not shown ) in jupyter notebook. after that i installed latest version. – Ankit gupta Feb 28 '20 at 12:17
  • 1
    Does this issue happen only in jupyter notebooks or outside of them too? – Newskooler Mar 29 '20 at 22:01