summaryrefslogtreecommitdiffstats
path: root/src/aristaproto/grpc/grpclib_server.py
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-07-29 09:40:12 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-07-29 09:40:12 +0000
commit14b40ec77a4bf8605789cc3aff0eb87625510a41 (patch)
tree4064d27144d6deaabfcd96df01bd996baa8b51a0 /src/aristaproto/grpc/grpclib_server.py
parentInitial commit. (diff)
downloadpython-aristaproto-14b40ec77a4bf8605789cc3aff0eb87625510a41.tar.xz
python-aristaproto-14b40ec77a4bf8605789cc3aff0eb87625510a41.zip
Adding upstream version 1.2+20240521.upstream/1.2+20240521upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/aristaproto/grpc/grpclib_server.py')
-rw-r--r--src/aristaproto/grpc/grpclib_server.py33
1 files changed, 33 insertions, 0 deletions
diff --git a/src/aristaproto/grpc/grpclib_server.py b/src/aristaproto/grpc/grpclib_server.py
new file mode 100644
index 0000000..3e28031
--- /dev/null
+++ b/src/aristaproto/grpc/grpclib_server.py
@@ -0,0 +1,33 @@
+from abc import ABC
+from collections.abc import AsyncIterable
+from typing import (
+ Any,
+ Callable,
+ Dict,
+)
+
+import grpclib
+import grpclib.server
+
+
+class ServiceBase(ABC):
+ """
+ Base class for async gRPC servers.
+ """
+
+ async def _call_rpc_handler_server_stream(
+ self,
+ handler: Callable,
+ stream: grpclib.server.Stream,
+ request: Any,
+ ) -> None:
+ response_iter = handler(request)
+ # check if response is actually an AsyncIterator
+ # this might be false if the method just returns without
+ # yielding at least once
+ # in that case, we just interpret it as an empty iterator
+ if isinstance(response_iter, AsyncIterable):
+ async for response_message in response_iter:
+ await stream.send_message(response_message)
+ else:
+ response_iter.close()