Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add context manager support for bentoml.client #3402

Merged
merged 2 commits into from
Jan 13, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 25 additions & 0 deletions src/bentoml/client.py
Expand Up @@ -20,6 +20,9 @@
from .exceptions import BentoMLException
from ._internal.service.inference_api import InferenceAPI

if t.TYPE_CHECKING:
from types import TracebackType


class Client(ABC):
server_url: str
Expand Down Expand Up @@ -85,6 +88,28 @@ def wait_until_server_is_ready(host: str, port: int, timeout: int) -> None:
raise TimeoutError("The server took too long to get ready")
time.sleep(1)

def __enter__(self):
return self

def __exit__(
self,
exc_type: type[BaseException] | None,
exc_value: BaseException | None,
traceback: TracebackType | None,
) -> bool | None:
pass

async def __aenter__(self):
return self

async def __aexit__(
self,
exc_type: type[BaseException] | None,
exc_value: BaseException | None,
traceback: TracebackType | None,
) -> bool | None:
pass

@staticmethod
def from_url(server_url: str) -> Client:
server_url = server_url if "://" in server_url else "http://" + server_url
Expand Down