V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
lizhien
V2EX  ›  Python

请教一下,为什么会输出`Unclosed connection`警告?

  •  
  •   lizhien · 2022-12-17 18:12:18 +08:00 · 1899 次点击
    这是一个创建于 494 天前的主题,其中的信息可能已经有所发展或是发生改变。

    请教一下,为什么会输出Unclosed connection警告?
    我在__aexit__明明已经close
    看官方的 issue 说是什么要aiohttp==4.0才能解决? 不知道我写的代码有没有问题
    现在我的解决方式屏蔽了这个warning
    麻烦各位了🥺

    版本

    • Python 3.9.15
    • aiohttp==3.8.3

    输出

    代码

    # -*- coding: utf-8 -*-
    import asyncio
    
    from aiohttp import ClientResponse, ClientTimeout
    
    from common import config
    
    from aiohttp_retry import RetryClient, ClientSession
    
    
    class Request:
    
        def __init__(self, *args, **kwargs):
            self.client_session = ClientSession()
            self.retry_client = RetryClient(client_session=self.client_session)
            self.request = self.retry_client.request(*args, **kwargs)
    
        async def __aenter__(self) -> ClientResponse:
            return await self.request
    
        async def __aexit__(self, exc_type, exc_val, exc_tb):
            await self.client_session.close()
            await self.retry_client.close()
    
    
    def get(url, params=None, headers=None):
        if headers is None:
            headers = {}
        if params is None:
            params = {}
        return request("GET", url, params=params, headers={
            **config.http['headers'], **headers
        })
    
    
    def request(method, url, params=None, headers=None, data=None):
        if headers is None:
            headers = {}
        if params is None:
            params = {}
        if data is None:
            data = {}
    
        return Request(
            method, config.http['http_proxy'] + url,
            params=params, proxy=config.http['proxy'],
            headers={**config.http['headers'], **headers},
            data=data, ssl=False,
            timeout=ClientTimeout(total=config.http['timeout']))
    
    
    def post(url, data=None, headers=None):
        if headers is None:
            headers = {}
        if data is None:
            data = {}
        return request("POST", url, data=data, headers={
            **config.http['headers'], **headers
        })
    
    
    if __name__ == '__main__':
        async def test():
            for i in range(100):
                async with request("GET", "https://www.baidu.com") as r:
                    print(r.status)
        asyncio.get_event_loop().run_until_complete(test())
    
    
    2 条回复    2022-12-18 21:35:42 +08:00
    ec0
        1
    ec0  
       2022-12-18 16:21:12 +08:00
    你的代码在我这里没有报 Unclosed connection

    版本
    Python 3.10.8
    aiohttp==3.8.3

    输出
    全是 200
    lizhien
        2
    lizhien  
    OP
       2022-12-18 21:35:42 +08:00
    @ec0 Python3.10 就没有问题,Python3.9 有,也不知道为什么
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3704 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 29ms · UTC 04:39 · PVG 12:39 · LAX 21:39 · JFK 00:39
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.