forked from bentoml/BentoML
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: serve missing logic from bentoml#3321
Signed-off-by: Aaron Pham <[email protected]>
- Loading branch information
Showing
2 changed files
with
104 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,58 @@ | ||
""" | ||
Server class for getting the Bento client and managing server process | ||
""" | ||
|
||
from __future__ import annotations | ||
|
||
import logging | ||
import traceback | ||
import subprocess | ||
from typing import TYPE_CHECKING | ||
|
||
import attr | ||
|
||
from ..utils import cached_property | ||
|
||
if TYPE_CHECKING: | ||
from types import TracebackType | ||
|
||
|
||
logger = logging.getLogger(__name__) | ||
|
||
class Server: | ||
def __init__(self, process: subprocess.Popen[bytes], host: str, port: int) -> None: | ||
self._process = process | ||
self._host = host | ||
self._port = port | ||
|
||
@attr.frozen | ||
class ServerHandle: | ||
process: subprocess.Popen[bytes] | ||
host: str | ||
port: int | ||
timeout: int = attr.field(default=10) | ||
|
||
@cached_property | ||
def client(self): | ||
return self.get_client() | ||
|
||
def get_client(self): | ||
from bentoml.client import Client | ||
|
||
Client.wait_until_server_is_ready(self._host, self._port, 10) | ||
return Client.from_url(f"http://localhost:{self._port}") | ||
Client.wait_until_server_is_ready( | ||
host=self.host, port=self.port, timeout=self.timeout | ||
) | ||
return Client.from_url(f"http://localhost:{self.port}") | ||
|
||
def stop(self) -> None: | ||
self.process.kill() | ||
|
||
@property | ||
def process(self) -> subprocess.Popen[bytes]: | ||
return self._process | ||
|
||
@property | ||
def address(self) -> str: | ||
return f"{self._host}:{self._port}" | ||
return f"{self.host}:{self.port}" | ||
|
||
def __enter__(self): | ||
yield self | ||
|
||
def __exit__( | ||
self, | ||
exc_type: type[BaseException], | ||
exc_value: BaseException, | ||
traceback_type: TracebackType, | ||
): | ||
try: | ||
self.stop() | ||
except Exception as e: # pylint: disable=broad-except | ||
logger.error(f"Error stopping server: {e}", exc_info=e) | ||
traceback.print_exception(exc_type, exc_value, traceback_type) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters