在 python 3.8 中使用 aiohttp 和 asyncio 关闭异常事件循环 [英] Exception event loop is closed with aiohttp and asyncio in python 3.8

查看:28
本文介绍了在 python 3.8 中使用 aiohttp 和 asyncio 关闭异常事件循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 asyncio 和 aiohttp 来发出并发请求.我最近将 Python 升级到 3.8.0 版,但在程序运行后出现 RuntimeError: Event loop is closed.

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())

我确实从 asyncio.gather() 得到了一个有效的结果,但是在退出时会引发异常.我想更改代码,以免出现异常.

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.

回溯如下:

Exception ignored in: <function _ProactorBasePipeTransport.__del__ at 0x000001E9A92079D0>
Traceback (most recent call last):
  File "C:UsersJonasAppDataLocalProgramsPythonPython38libasyncioproactor_events.py", line 116, in __del__
    self.close()
  File "C:UsersJonasAppDataLocalProgramsPythonPython38libasyncioproactor_events.py", line 108, in close
    self._loop.call_soon(self._call_connection_lost, None)
  File "C:UsersJonasAppDataLocalProgramsPythonPython38libasyncioase_events.py", line 711, in call_soon
    self._check_closed()
  File "C:UsersJonasAppDataLocalProgramsPythonPython38libasyncioase_events.py", line 504, in _check_closed
    raise RuntimeError('Event loop is closed')
RuntimeError: Event loop is closed`

推荐答案

我认为这很可能是 aiohttp 错误.具体来说,我在他们的 github 上发现了这个问题:https://github.com/aio-libs/aiohttp/issues/4324

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!

这篇关于在 python 3.8 中使用 aiohttp 和 asyncio 关闭异常事件循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆