From 853def95cd28eff0e2a87f6e99479447bad537a1 Mon Sep 17 00:00:00 2001 From: Aaron Pham <29749331+aarnphm@users.noreply.github.com> Date: Mon, 13 Nov 2023 18:11:17 -0500 Subject: [PATCH] fix(client): correct destructor the httpx object boht sync and async (#636) Signed-off-by: Aaron <29749331+aarnphm@users.noreply.github.com> --- openllm-client/src/openllm_client/_http.py | 3 --- openllm-client/src/openllm_client/_shim.py | 14 ++++++++++++++ 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/openllm-client/src/openllm_client/_http.py b/openllm-client/src/openllm_client/_http.py index dc68a460..5d2addb6 100644 --- a/openllm-client/src/openllm_client/_http.py +++ b/openllm-client/src/openllm_client/_http.py @@ -46,9 +46,6 @@ class HTTPClient(Client): return {'Authorization': f'Bearer {env}'} return super()._build_auth_headers() - def __del__(self): - self.close() - @property def _metadata(self): if self.__metadata is None: diff --git a/openllm-client/src/openllm_client/_shim.py b/openllm-client/src/openllm_client/_shim.py index 88c7b2a3..4c7470e7 100644 --- a/openllm-client/src/openllm_client/_shim.py +++ b/openllm-client/src/openllm_client/_shim.py @@ -1,5 +1,6 @@ # This provides a base shim with httpx and acts as base request from __future__ import annotations +import asyncio import email.utils import logging import platform @@ -374,6 +375,9 @@ class Client(BaseClient[httpx.Client, Stream[t.Any]]): def __exit__(self, *args) -> None: self.close() + def __del__(self): + self.close() + def request( self, response_cls: type[Response], @@ -504,6 +508,16 @@ class AsyncClient(BaseClient[httpx.AsyncClient, AsyncStream[t.Any]]): async def __aexit__(self, *args) -> None: await self.close() + def __del__(self): + try: + loop = asyncio.get_event_loop() + if loop.is_running(): + loop.create_task(self.close()) + else: + loop.run_until_complete(self.close()) + except Exception: + pass + async def request( self, response_cls: type[Response],