From cef8dbf27c1c074f7d11681ffb4ae3805d727599 Mon Sep 17 00:00:00 2001 From: Tim Hughes Date: Thu, 1 Oct 2020 00:05:11 +0100 Subject: [PATCH] Adds python aio helloworld examples --- .../python/helloworld/async_greeter_client.py | 37 ++++++++++++++++ .../python/helloworld/async_greeter_server.py | 43 +++++++++++++++++++ 2 files changed, 80 insertions(+) create mode 100644 examples/python/helloworld/async_greeter_client.py create mode 100644 examples/python/helloworld/async_greeter_server.py diff --git a/examples/python/helloworld/async_greeter_client.py b/examples/python/helloworld/async_greeter_client.py new file mode 100644 index 00000000000..e86cd30f8ea --- /dev/null +++ b/examples/python/helloworld/async_greeter_client.py @@ -0,0 +1,37 @@ +# Copyright 2015 gRPC authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +"""The Python implementation of the GRPC helloworld.Greeter client.""" + +import logging +import asyncio +import grpc + +import helloworld_pb2 +import helloworld_pb2_grpc + + +async def run(): + # NOTE(gRPC Python Team): .close() is possible on a channel and should be + # used in circumstances in which the with statement does not fit the needs + # of the code. + async with grpc.aio.insecure_channel('localhost:50051') as channel: + stub = helloworld_pb2_grpc.GreeterStub(channel) + response = await stub.SayHello(helloworld_pb2.HelloRequest(name='you')) + print("Greeter client received: " + response.message) + + +if __name__ == '__main__': + logging.basicConfig() + asyncio.run(run()) + diff --git a/examples/python/helloworld/async_greeter_server.py b/examples/python/helloworld/async_greeter_server.py new file mode 100644 index 00000000000..79903aed8d7 --- /dev/null +++ b/examples/python/helloworld/async_greeter_server.py @@ -0,0 +1,43 @@ +# Copyright 2015 gRPC authors. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. +"""The Python implementation of the GRPC helloworld.Greeter server.""" + +from concurrent import futures +import logging +import asyncio +import grpc + +import helloworld_pb2 +import helloworld_pb2_grpc + + +class Greeter(helloworld_pb2_grpc.GreeterServicer): + + def SayHello(self, request, context): + return helloworld_pb2.HelloReply(message='Hello, %s!' % request.name) + + +async def serve(): + server = grpc.aio.server(futures.ThreadPoolExecutor(max_workers=10)) + helloworld_pb2_grpc.add_GreeterServicer_to_server(Greeter(), server) + listen_addr = '[::]:50051' + server.add_insecure_port(listen_addr) + logging.info(f"Starting server on {listen_addr}") + await server.start() + await server.wait_for_termination() + + +if __name__ == '__main__': + logging.basicConfig(level=logging.INFO) + asyncio.run(serve())