Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

asyncio: RuntimeError this event loop is already running

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 ?

like image 649
lhk Avatar asked Nov 11 '18 11:11

lhk


People also ask

What is error 18 asyncio runtimeerror?

18 Asyncio RuntimeError: Event Loop is Closed 0 aiohttp: calling asyncio from a running web.Application: RuntimeError: This event loop is already running 12 python aiohttp into existing event loop

Can asyncio event loop be nested?

May 11 at 9:28 Citing nest_asyncio: By design asyncio does not allow its event loop to be nested. This presents a practical problem: When in an environment where the event loop is already running it’s impossible to run tasks and wait for the result.

Why is asyncio run() not working?

The aforementioned issue can come up when using the asyncio.run () or the loop.run_until_complete () functions. The documentation suggests using run () over run_until_complete () because run () handles the setting and closing of the event loop object. The run () command is actually just a wrapper around the run_until_complete () command.

Why do I get runtimeerror event loop is closed?

When you’re running an async / await function using the asyncio library, you may get a RuntimeError: Event Loop is closed error even after your loop is already done running. This problem may not always affect the functionality of a single script but will affect the functionality of multiple scripts.


1 Answers

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.

like image 174
kayoz Avatar answered Oct 14 '22 02:10

kayoz