aiohttp出现 raise RuntimeError(‘Event loop is closed‘) 来自(<function _ProactorBasePipeTransport.__del_)

Python aiohttp raise RuntimeError(‘Event loop is closed‘)

报错内容

aiohttp的一个抓取网页链接的script在Linux 和 Mac 上这样运行是没问题的,但是在 Windows 上运行会报类似如下错误:

Exception ignored in: <function _ProactorBasePipeTransport.__del__ at 0x000001DC185B0310>
Traceback (most recent call last):
  File "C:\Program Files\Python310\lib\asyncio\proactor_events.py", line 116, in __del__
    self.close()
  File "C:\Program Files\Python310\lib\asyncio\proactor_events.py", line 108, in close
    self._loop.call_soon(self._call_connection_lost, None)
  File "C:\Program Files\Python310\lib\asyncio\base_events.py", line 750, in call_soon
    self._check_closed()
  File "C:\Program Files\Python310\lib\asyncio\base_events.py", line 515, in _check_closed
    raise RuntimeError('Event loop is closed')
RuntimeError: Event loop is closed

参考内容

详细解释来自 Python aiohttp raise RuntimeError(‘Event loop is closed‘)

以下内容搬运过来了:(如有侵权或者不舒服,可联系我撤下,狗头保命)

原因分析

像 aiohttp 这类第三方协程库都是依赖于标准库 asyncio 的,而 asyncio 对 Windows 的支持本来就不好。Python3.8 后默认 Windows 系统上的事件循环采用 ProactorEventLoop (仅用于 Windows )这篇文档描述了其在 Windows 下的缺陷:https://docs.python.org/zh-cn/3/library/asyncio-platforms.html#windows 👈

引发异常的函数是 ProactorBasePipeTransport.del ,所以 aiohttp 铁定使用了 ProactorBasePipeTransport,并且在程序退出释放内存时自动调用了其__del 方法
proactor events
在这里插入图片描述
在这里插入图片描述

就是上述一串连环反应最终抛出了 RuntimeError: Event loop is closed

解决方案

如果执意要在 Windows 下继续开发,有这几个方案可以选择

1. 不要使用 run 函数

既然 _ProactorBasePipeTransport 会在程序结束后自动关闭事件循环,那就不要用 run 函数了,用官网的例子,乖乖使用 loop 吧

loop = asyncio.get_event_loop()
loop.run_until_complete(main())
2. 替换事件循环

在调用 run 函数前,替换默认的 ProactorEventLoop 为 SelectorEventLoop

asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())
asyncio.run(main())

但是 SelectorEventLoop 是有一些缺点的,比如不支持子进程等

3. 忽略异常

这是 Github 上一个外国大佬的方法,在不改变源码的前提下,使用装饰器忽略掉异常

import asyncio
from asyncio.proactor_events import _ProactorBasePipeTransport
from functools import wraps
 
import aiohttp
 
 
async def main():
    async with aiohttp.ClientSession() as session:
        async with session.get('https://www.baidu.com') as response:
            print("Status:", response.status)
            print("Content-type:", response.headers['content-type'])
 
            html = await response.text()
            print("Body:", html[:15], "...")
 
 
def silence_event_loop_closed(func):
    @wraps(func)
    def wrapper(self, *args, **kwargs):
        try:
            return func(self, *args, **kwargs)
        except RuntimeError as e:
            if str(e) != 'Event loop is closed':
                raise
 
    return wrapper
 
 
_ProactorBasePipeTransport.__del__ = silence_event_loop_closed(_ProactorBasePipeTransport.__del__)
 
asyncio.run(main())

更详细的信息可以在这个 issue 上找到:https://github.com/aio-libs/aiohttp/issues/4324 👈

相关链接:

https://github.com/aio-libs/aiohttp/issues/4324
https://stackoverflow.com/questions/45600579/asyncio-event-loop-is-closed-when-getting-loop
https://docs.python.org/zh-cn/3/library/asyncio-platforms.html#windows
https://bugs.python.org/issue39232

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值