diff --git a/examples/_reference/client.py b/examples/_reference/client.py index 7041678..b7797b1 100644 --- a/examples/_reference/client.py +++ b/examples/_reference/client.py @@ -1,18 +1,20 @@ -import grpc +import asyncio + +import grpc.experimental.aio as grpc_aio from helloworld import helloworld_pb2 from helloworld import helloworld_pb2_grpc -def main(): - channel = grpc.insecure_channel('127.0.0.1:50051') - stub = helloworld_pb2_grpc.GreeterStub(channel) - - print(stub.SayHello(helloworld_pb2.HelloRequest(name='World'))) +async def main(): + async with grpc_aio.insecure_channel('127.0.0.1:50051') as channel: + stub = helloworld_pb2_grpc.GreeterStub(channel) + reply = await stub.SayHello(helloworld_pb2.HelloRequest(name='World')) + print(reply) if __name__ == '__main__': try: - main() + asyncio.run(main()) except KeyboardInterrupt: pass diff --git a/examples/_reference/server.py b/examples/_reference/server.py index 4f1f514..343028f 100644 --- a/examples/_reference/server.py +++ b/examples/_reference/server.py @@ -1,7 +1,6 @@ -import time -import concurrent.futures +import asyncio -import grpc +import grpc.experimental.aio as grpc_aio from helloworld import helloworld_pb2 from helloworld import helloworld_pb2_grpc @@ -9,25 +8,24 @@ from helloworld import helloworld_pb2_grpc class Greeter(helloworld_pb2_grpc.GreeterServicer): - def SayHello(self, request, context): + async def SayHello(self, request, context): return helloworld_pb2.HelloReply(message='Hello, %s!' % request.name) -def serve(host='127.0.0.1', port=50051): - server = grpc.server(concurrent.futures.ThreadPoolExecutor(max_workers=10)) +async def serve(host='127.0.0.1', port=50051): + server = grpc_aio.server() helloworld_pb2_grpc.add_GreeterServicer_to_server(Greeter(), server) server.add_insecure_port(f'{host}:{port}') - server.start() + await server.start() print(f'Serving on {host}:{port}') try: - while True: - time.sleep(3600) + await server.wait_for_termination() finally: - server.stop(0) + await server.stop(10) if __name__ == '__main__': try: - serve() + asyncio.run(serve()) except KeyboardInterrupt: pass diff --git a/examples/helloworld/helloworld_pb2_grpc.py b/examples/helloworld/helloworld_pb2_grpc.py index 51b2a74..8b2fe08 100644 --- a/examples/helloworld/helloworld_pb2_grpc.py +++ b/examples/helloworld/helloworld_pb2_grpc.py @@ -5,42 +5,60 @@ from helloworld import helloworld_pb2 as helloworld_dot_helloworld__pb2 class GreeterStub(object): - # missing associated documentation comment in .proto file - pass + """Missing associated documentation comment in .proto file""" - def __init__(self, channel): - """Constructor. + def __init__(self, channel): + """Constructor. - Args: - channel: A grpc.Channel. - """ - self.SayHello = channel.unary_unary( - '/helloworld.Greeter/SayHello', - request_serializer=helloworld_dot_helloworld__pb2.HelloRequest.SerializeToString, - response_deserializer=helloworld_dot_helloworld__pb2.HelloReply.FromString, - ) + Args: + channel: A grpc.Channel. + """ + self.SayHello = channel.unary_unary( + '/helloworld.Greeter/SayHello', + request_serializer=helloworld_dot_helloworld__pb2.HelloRequest.SerializeToString, + response_deserializer=helloworld_dot_helloworld__pb2.HelloReply.FromString, + ) class GreeterServicer(object): - # missing associated documentation comment in .proto file - pass + """Missing associated documentation comment in .proto file""" - def SayHello(self, request, context): - # missing associated documentation comment in .proto file - pass - context.set_code(grpc.StatusCode.UNIMPLEMENTED) - context.set_details('Method not implemented!') - raise NotImplementedError('Method not implemented!') + def SayHello(self, request, context): + """Missing associated documentation comment in .proto file""" + context.set_code(grpc.StatusCode.UNIMPLEMENTED) + context.set_details('Method not implemented!') + raise NotImplementedError('Method not implemented!') def add_GreeterServicer_to_server(servicer, server): - rpc_method_handlers = { - 'SayHello': grpc.unary_unary_rpc_method_handler( - servicer.SayHello, - request_deserializer=helloworld_dot_helloworld__pb2.HelloRequest.FromString, - response_serializer=helloworld_dot_helloworld__pb2.HelloReply.SerializeToString, - ), - } - generic_handler = grpc.method_handlers_generic_handler( - 'helloworld.Greeter', rpc_method_handlers) - server.add_generic_rpc_handlers((generic_handler,)) + rpc_method_handlers = { + 'SayHello': grpc.unary_unary_rpc_method_handler( + servicer.SayHello, + request_deserializer=helloworld_dot_helloworld__pb2.HelloRequest.FromString, + response_serializer=helloworld_dot_helloworld__pb2.HelloReply.SerializeToString, + ), + } + generic_handler = grpc.method_handlers_generic_handler( + 'helloworld.Greeter', rpc_method_handlers) + server.add_generic_rpc_handlers((generic_handler,)) + + + # This class is part of an EXPERIMENTAL API. +class Greeter(object): + """Missing associated documentation comment in .proto file""" + + @staticmethod + def SayHello(request, + target, + options=(), + channel_credentials=None, + call_credentials=None, + compression=None, + wait_for_ready=None, + timeout=None, + metadata=None): + return grpc.experimental.unary_unary(request, target, '/helloworld.Greeter/SayHello', + helloworld_dot_helloworld__pb2.HelloRequest.SerializeToString, + helloworld_dot_helloworld__pb2.HelloReply.FromString, + options, channel_credentials, + call_credentials, compression, wait_for_ready, timeout, metadata) diff --git a/scripts/bench.py b/scripts/bench.py index 6c75a37..cf6000a 100644 --- a/scripts/bench.py +++ b/scripts/bench.py @@ -49,7 +49,7 @@ async def _grpclib_server(*, host='127.0.0.1', port=50051): @serve.command('grpclib') def serve_grpclib(): - asyncio.get_event_loop().run_until_complete(_grpclib_server()) + asyncio.run(_grpclib_server()) @serve.command('grpclib+uvloop') @@ -57,14 +57,28 @@ def serve_grpclib_uvloop(): import uvloop uvloop.install() - asyncio.get_event_loop().run_until_complete(_grpclib_server()) + asyncio.run(_grpclib_server()) @serve.command('grpcio') def serve_grpcio(): from _reference.server import serve + try: - serve() + asyncio.run(serve()) + except KeyboardInterrupt: + pass + + +@serve.command('grpcio+uvloop') +def serve_grpcio_uvloop(): + import uvloop + uvloop.install() + + from _reference.server import serve + + try: + asyncio.run(serve()) except KeyboardInterrupt: pass