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 ?