grpclib/example/streaming/server.py

56 lines
1.8 KiB
Python
Raw Normal View History

2018-08-20 14:07:33 +00:00
import asyncio
from grpclib.utils import graceful_exit
2018-08-20 14:07:33 +00:00
from grpclib.server import Server
from .helloworld_pb2 import HelloReply
from .helloworld_grpc import GreeterBase
class Greeter(GreeterBase):
# UNARY_UNARY - simple RPC
async def UnaryUnaryGreeting(self, stream):
request = await stream.recv_message()
message = f'Hello, {request.name}!'
2018-08-20 14:07:33 +00:00
await stream.send_message(HelloReply(message=message))
# UNARY_STREAM - response streaming RPC
async def UnaryStreamGreeting(self, stream):
request = await stream.recv_message()
await stream.send_message(
HelloReply(message=f'Hello, {request.name}!'))
2018-08-20 14:07:33 +00:00
await stream.send_message(
HelloReply(message=f'Goodbye, {request.name}!'))
2018-08-20 14:07:33 +00:00
# STREAM_UNARY - request streaming RPC
async def StreamUnaryGreeting(self, stream):
names = []
async for request in stream:
names.append(request.name)
message = 'Hello, {}!'.format(' and '.join(names))
await stream.send_message(HelloReply(message=message))
# STREAM_STREAM - bidirectional streaming RPC
async def StreamStreamGreeting(self, stream):
async for request in stream:
message = f'Hello, {request.name}!'
2018-08-20 14:07:33 +00:00
await stream.send_message(HelloReply(message=message))
# Send another message to demonstrate responses are not
# coupled to requests.
message = 'Goodbye, all!'
await stream.send_message(HelloReply(message=message))
async def main(*, host='127.0.0.1', port=50051):
loop = asyncio.get_running_loop()
server = Server([Greeter()], loop=loop)
with graceful_exit([server], loop=loop):
await server.start(host, port)
print(f'Serving on {host}:{port}')
2018-08-20 14:07:33 +00:00
await server.wait_closed()
if __name__ == '__main__':
asyncio.run(main())