请教一下,为什么会输出Unclosed connection
警告?
我在__aexit__
明明已经close
了
看官方的 issue 说是什么要aiohttp==4.0
才能解决?
不知道我写的代码有没有问题
现在我的解决方式屏蔽了这个warning
麻烦各位了🥺
# -*- 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())
1
ec0 2022-12-18 16:21:12 +08:00
你的代码在我这里没有报 Unclosed connection
版本 Python 3.10.8 aiohttp==3.8.3 输出 全是 200 |