Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: sync and async client implementation
Signed-off-by: Ubuntu <29749331+aarnphm@users.noreply.github.com>
- Loading branch information
Showing
3 changed files
with
284 additions
and
79 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,33 +1,80 @@ | ||
from __future__ import annotations | ||
|
||
import asyncio | ||
import functools | ||
from typing import TYPE_CHECKING | ||
|
||
import numpy as np | ||
|
||
from bentoml.client import Client | ||
|
||
if TYPE_CHECKING: | ||
from bentoml.client import GrpcClient | ||
|
||
async def run(): | ||
c = Client.from_url("localhost:3000", grpc=True) | ||
async with c.aservice("grpc.health.v1.Health") as health_client: | ||
print(await health_client.Check(service="bentoml.grpc.v1.BentoService")) | ||
|
||
async def arun(c: GrpcClient): | ||
print("registered services:", await c.get_services()) | ||
|
||
async with c.aservice("health") as health_client: | ||
res = await health_client.Check(service="bentoml.grpc.v1.BentoService") | ||
print(res) | ||
|
||
async with c.aservice() as client: | ||
print( | ||
await client.Call( | ||
api_name="classify", | ||
ndarray=dict( | ||
dtype=1, | ||
shape=[1, 4], | ||
float_values=[5.9, 3, 5.1, 1.8], | ||
), | ||
return_proto=True, | ||
res = await client.Call( | ||
api_name="classify", | ||
ndarray=dict( | ||
dtype=1, | ||
shape=[1, 4], | ||
float_values=[5.9, 3, 5.1, 1.8], | ||
), | ||
) | ||
print("Result from 'await client.Call':\n", res) | ||
res = await client.async_classify(np.array([[5.9, 3, 5.1, 1.8]])) | ||
print("Result from 'client.async_classify':\n", res) | ||
|
||
|
||
def run(c: Client): | ||
with c.service("health") as health_client: | ||
Check = functools.partial( | ||
health_client.Check, service="bentoml.grpc.v1.BentoService" | ||
) | ||
for to_json in (True, False): | ||
print( | ||
f"Health check ({'serialized' if to_json else 'deserialized'}):", | ||
Check(to_json=to_json), | ||
) | ||
|
||
with c.service() as client: | ||
res = client.Call( | ||
api_name="classify", | ||
ndarray=dict( | ||
dtype=1, | ||
shape=[1, 4], | ||
float_values=[5.9, 3, 5.1, 1.8], | ||
), | ||
) | ||
print("Result from 'client.Call':\n", res) | ||
res = client.classify(np.array([[5.9, 3, 5.1, 1.8]])) | ||
print("Result from 'client.classify':\n", res) | ||
res = client.call("classify", np.array([[5.9, 3, 5.1, 1.8]])) | ||
print("Result from 'client.call(bentoml_api_name='classify')':\n", res) | ||
|
||
|
||
if __name__ == "__main__": | ||
loop = asyncio.new_event_loop() | ||
try: | ||
loop.run_until_complete(run()) | ||
finally: | ||
loop.close() | ||
assert loop.is_closed() | ||
import argparse | ||
|
||
parser = argparse.ArgumentParser() | ||
parser.add_argument("-rwa", "--run-with-async", action="store_true") | ||
args = parser.parse_args() | ||
|
||
c = Client.from_url("localhost:3000", grpc=True) | ||
|
||
if args.run_with_async: | ||
loop = asyncio.new_event_loop() | ||
try: | ||
loop.run_until_complete(arun(c)) | ||
finally: | ||
loop.close() | ||
assert loop.is_closed() | ||
else: | ||
run(c) |
Oops, something went wrong.