Skip to content

Commit

Permalink
fix: anyio.run_sync for sync function
Browse files Browse the repository at this point in the history
Signed-off-by: Aaron Pham <29749331+aarnphm@users.noreply.github.com>
  • Loading branch information
aarnphm committed Jul 27, 2022
1 parent 66b5156 commit e615355
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
9 changes: 8 additions & 1 deletion bentoml/_internal/io_descriptors/numpy.py
Expand Up @@ -223,7 +223,7 @@ def __init__(
enforce_dtype: bool = False,
shape: tuple[int, ...] | None = None,
enforce_shape: bool = False,
packed: bool = True,
packed: bool = False,
bytesorder: t.Literal["C", "F", "A", None] = None,
):
if dtype is not None and not isinstance(dtype, np.dtype):
Expand Down Expand Up @@ -441,6 +441,7 @@ async def to_grpc_response(
Protobuf representation of given `np.ndarray`
"""
from ..utils.grpc import grpc_status_code
from ..configuration import get_debug_mode

_NPTYPE_TO_DTYPE_STRING_MAP = {
np.dtype(v): k for k, v in _DTYPE_TO_STRING_MAP.items()
Expand All @@ -460,6 +461,10 @@ async def to_grpc_response(
if self._packed:
cnt.update({"bytes_contents": obj.tobytes(order=self._bytesorder)})
else:
if self._bytesorder:
logger.warning(
f"'bytesorder={self._bytesorder}' is ignored when 'packed={self._packed}'."
)
cnt.update({_DTYPE_TO_FIELD_MAP[dtype_string]: obj.tolist()})

if obj.ndim == 1:
Expand All @@ -472,6 +477,8 @@ async def to_grpc_response(
shape=tuple(obj.shape), array=service_pb2.Array(**cnt)
)
)
if get_debug_mode():
logger.debug(f"Response proto: \n{resp}")
return resp

def generate_protobuf(self):
Expand Down
3 changes: 2 additions & 1 deletion bentoml/_internal/server/grpc/servicer.py
Expand Up @@ -3,6 +3,7 @@
import asyncio
from typing import TYPE_CHECKING

import anyio
from grpc import aio

from bentoml.exceptions import UnprocessableEntity
Expand Down Expand Up @@ -41,7 +42,7 @@ async def Infer( # type: ignore (no async types)
if asyncio.iscoroutinefunction(api.func):
output = await api.func(input)
else:
output = api.func(input)
output = await anyio.to_thread.run_sync(api.func, input)

return await api.output.to_grpc_response(output, context)

Expand Down

0 comments on commit e615355

Please sign in to comment.