fix httpx and aiohttp issues

It seems that we no longer need to close the sessions.
This commit is contained in:
lilydjwg 2022-04-07 13:44:38 +08:00
parent 2ea44d3694
commit 2e042d7576
2 changed files with 12 additions and 18 deletions

View file

@ -1,7 +1,6 @@
# MIT licensed # MIT licensed
# Copyright (c) 2013-2020 lilydjwg <lilydjwg@gmail.com>, et al. # Copyright (c) 2013-2020 lilydjwg <lilydjwg@gmail.com>, et al.
import atexit
import asyncio import asyncio
from typing import Optional, Dict from typing import Optional, Dict
@ -16,16 +15,15 @@ logger = structlog.get_logger(logger_name=__name__)
connector = aiohttp.TCPConnector(limit=20) connector = aiohttp.TCPConnector(limit=20)
class AiohttpSession(BaseSession): class AiohttpSession(BaseSession):
session = None
def setup( def setup(
self, self,
concurreny: int = 20, concurreny: int = 20,
timeout: int = 20, timeout: int = 20,
) -> None: ) -> None:
self.session = aiohttp.ClientSession( self._concurreny = concurreny
connector = aiohttp.TCPConnector(limit=concurreny), self._timeout = timeout
timeout = aiohttp.ClientTimeout(total=timeout),
trust_env = True,
)
async def request_impl( async def request_impl(
self, url: str, *, self, url: str, *,
@ -38,6 +36,14 @@ class AiohttpSession(BaseSession):
body = None, body = None,
verify_cert: bool = True, verify_cert: bool = True,
) -> Response: ) -> Response:
if self.session is None:
# need to create in async context
self.session = aiohttp.ClientSession(
connector = aiohttp.TCPConnector(limit=self._concurreny),
timeout = aiohttp.ClientTimeout(total=self._timeout),
trust_env = True,
)
kwargs = { kwargs = {
'headers': headers, 'headers': headers,
'params': params, 'params': params,
@ -77,9 +83,4 @@ class AiohttpSession(BaseSession):
body = await res.content.read() body = await res.content.read()
return Response(res.headers, body) return Response(res.headers, body)
@atexit.register
def cleanup():
loop = asyncio.get_event_loop()
loop.run_until_complete(session.session.close())
session = AiohttpSession() session = AiohttpSession()

View file

@ -1,7 +1,6 @@
# MIT licensed # MIT licensed
# Copyright (c) 2020 lilydjwg <lilydjwg@gmail.com>, et al. # Copyright (c) 2020 lilydjwg <lilydjwg@gmail.com>, et al.
import atexit
from typing import Dict, Optional, Tuple from typing import Dict, Optional, Tuple
import httpx import httpx
@ -75,10 +74,4 @@ class HttpxSession(BaseSession):
await client.aclose() await client.aclose()
del self.clients del self.clients
@atexit.register
def cleanup():
import asyncio
loop = asyncio.get_event_loop()
loop.run_until_complete(session.aclose())
session = HttpxSession() session = HttpxSession()