Updated reference examples and benchmarks to use experimental asyncio support from grpcio

This commit is contained in:
Vladimir Magamedov 2020-06-05 23:19:44 +03:00
parent 3eb9f3110e
commit 29f817b98e
4 changed files with 83 additions and 51 deletions

View File

@ -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

View File

@ -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

View File

@ -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)

View File

@ -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