11

This seems like a common problem. See for example: Error "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?

2
  • 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. Commented Nov 11, 2018 at 11:53
  • That's what I thought, too (this is jupyter :P) but I restarted the kernel before running this. Commented Nov 11, 2018 at 13:20

1 Answer 1

12

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.

Sign up to request clarification or add additional context in comments.

3 Comments

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.
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.
Does this issue happen only in jupyter notebooks or outside of them too?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.