gpt4 book ai didi

python - aiohttp - 异常忽略消息

转载 作者:太空狗 更新时间:2023-10-29 18:28:47 25 4
gpt4 key购买 nike

我正在运行以下代码,它通过 aiohttp 发出 5 个请求:

import aiohttp
import asyncio

def fetch_page(url, idx):
try:
url = 'http://google.com'
response = yield from aiohttp.request('GET', url)

print(response.status)
except Exception as e:
print(e)

def main():
try:
url = 'http://google.com'
urls = [url] * 5

coros = []
for idx, url in enumerate(urls):
coros.append(asyncio.Task(fetch_page(url, idx)))

yield from asyncio.gather(*coros)
except Exception as e:
print(e)

if __name__ == '__main__':
try:
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
except Exception as e:
print(e)

输出:

200
200
200
200
200
Exception ignored in: Exception ignored in: Exception ignored in: Exception ignored in: Exception ignored in:

注意:没有关于异常是什么/在哪里的额外信息。

这是什么原因造成的,有什么调试技巧吗?

最佳答案

我不确定为什么,但似乎让 aiohttp.ClientResponse 对象保持打开状态会导致在解释器退出时抛出无法引发的异常。在我的系统上,这会导致这样的警告,而不是“忽略异常”消息:

sys:1: ResourceWarning: unclosed <socket object at 0x7f44fce557a8>
sys:1: ResourceWarning: unclosed <socket object at 0x7f44fce55718>
sys:1: ResourceWarning: unclosed <socket object at 0x7f44fcc24a78>
sys:1: ResourceWarning: unclosed <socket object at 0x7f44fcc248c8>
sys:1: ResourceWarning: unclosed <socket object at 0x7f44fcc24958>
sys:1: ResourceWarning: unclosed <socket object at 0x7f44fcc249e8>
sys:1: ResourceWarning: unclosed <socket object at 0x7f44fcc24b08>

在任何情况下,您都可以通过调用 response.close() 显式关闭 fetch_objects 末尾的 ClientResponse 对象来修复它>.

关于python - aiohttp - 异常忽略消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29502779/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com