7

I am using asyncio and aiohttp to make concurrent requests. I've recently upgraded Python to version 3.8.0 and I'm getting a RuntimeError: Event loop is closed after the program has run.

import asyncio
import aiohttp

async def do_call(name, session):
    async with session.get('https://www.google.be') as response:
        await response.text()
        return 'ok - {}'.format(name)

async def main():
    async with aiohttp.ClientSession() as session:
        tasks = [do_call(str(i), session) for i in range(0,4)]
        results = await asyncio.gather(*tasks)
        print(results)

asyncio.run(main())

I do get a valid result from asyncio.gather(), but when exiting the exception is raised. I'd like to change the code so it doesn't run into exceptions.

The traceback is as follows:

Exception ignored in: <function _ProactorBasePipeTransport.__del__ at 0x000001E9A92079D0>
Traceback (most recent call last):
  File "C:\Users\Jonas\AppData\Local\Programs\Python\Python38\lib\asyncio\proactor_events.py", line 116, in __del__
    self.close()
  File "C:\Users\Jonas\AppData\Local\Programs\Python\Python38\lib\asyncio\proactor_events.py", line 108, in close
    self._loop.call_soon(self._call_connection_lost, None)
  File "C:\Users\Jonas\AppData\Local\Programs\Python\Python38\lib\asyncio\base_events.py", line 711, in call_soon
    self._check_closed()
  File "C:\Users\Jonas\AppData\Local\Programs\Python\Python38\lib\asyncio\base_events.py", line 504, in _check_closed
    raise RuntimeError('Event loop is closed')
RuntimeError: Event loop is closed`
depken
  • 73
  • 1
  • 5
  • could you post the full traceback for the RuntimeError? It would be helpful :) – PirateNinjas Dec 10 '19 at 10:30
  • your example runs for me without error. Is there some other piece of code related to this that might be causing the error? – PirateNinjas Dec 10 '19 at 10:45
  • @PirateNinjas, thanks for your reply. The full program is posted in this question. I've added the traceback to the question. – depken Dec 10 '19 at 12:16
  • which version of aiohttp are you using? I have your code working with python=3.8.0 and aiohttp=3.6.2 – PirateNinjas Dec 10 '19 at 12:21
  • @PirateNinjas I'm also using 3.6.2, running on windows 10. Are you getting exceptions at the end of the program? – depken Dec 10 '19 at 12:28
  • I get no exceptions - the program just prints the list of "ok ###" as expected. I'm using ubuntu 18.04. Perhaps that's a key difference here. – PirateNinjas Dec 10 '19 at 12:30

2 Answers2

13

I think this is most likely an aiohttp bug. Specifically, I found this issue on their github: https://github.com/aio-libs/aiohttp/issues/4324

I realise this doesn't necessarily help you, but maybe you can switch back and stop banging your head against a wall. Your code is fine!

PirateNinjas
  • 1,908
  • 1
  • 16
  • 21
  • 6
    +1 for the link to the issue: [replacing `asyncio.run(...)` by `asyncio.get_event_loop().run_until_complete(...)`](https://github.com/aio-libs/aiohttp/issues/4324#issuecomment-676675779) did the trick for me. – Stef Dec 07 '20 at 15:50
-1

I solved this by not calling my_loop.close() after I was done using it. Closing the event loop this way caused the error to be thrown even after I had gotten all the responses I was expecting.

Edvard-D
  • 384
  • 1
  • 6
  • 17
  • 2
    Unfortunately I don't think this applies to the question above. The Questioner has used asyncio.run rather than getting the event loop, which wraps the event loop closure into its behaviour. I.e. using run, you don't ever call close, so you can't make use of your answer. – PirateNinjas Dec 08 '20 at 14:39