Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Exception event loop is closed with aiohttp and asyncio in python 3.8

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`
like image 483
depken Avatar asked Dec 09 '19 19:12

depken


People also ask

How do I stop Asyncio from looping events?

Run an asyncio Event Loop run_until_complete(<some Future object>) – this function runs a given Future object, usually a coroutine defined by the async / await pattern, until it's complete. run_forever() – this function runs the loop forever. stop() – the stop function stops a running loop.

What version of Python has Asyncio?

asyncio requires Python 3.3 or later! The asyncio module is part of the Python standard library since Python 3.4. asyncio is a free software distributed under the Apache license version 2.0.

What is event loop in Asyncio?

The event loop is the core of every asyncio application. Event loops run asynchronous tasks and callbacks, perform network IO operations, and run subprocesses. Application developers should typically use the high-level asyncio functions, such as asyncio.

Is Asyncio in standard library?

The asyncio package has been included in the standard library on a provisional basis.


1 Answers

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!

like image 165
PirateNinjas Avatar answered Sep 19 '22 11:09

PirateNinjas