Skip to content

Commit

Permalink
feat: sync and async client implementation
Browse files Browse the repository at this point in the history
Signed-off-by: Ubuntu <29749331+aarnphm@users.noreply.github.com>
  • Loading branch information
aarnphm committed Nov 28, 2022
1 parent ce457b3 commit aca74f0
Show file tree
Hide file tree
Showing 3 changed files with 284 additions and 79 deletions.
85 changes: 66 additions & 19 deletions grpc-client/python/client.py
@@ -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)

0 comments on commit aca74f0

Please sign in to comment.