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],