Open
Description
There're a few issues with the example server, even after using the prerelease version. Probably not worth a PR, so here's an update example with notes below:
import asyncio
from echo import EchoBase, EchoResponse, EchoStreamResponse
from grpclib.server import Server
from typing import AsyncIterator
class EchoService(EchoBase):
async def echo(self, value: str, extra_times: int) -> EchoResponse:
return EchoResponse(values=[value] * extra_times)
async def echo_stream(
self, value: str, extra_times: int
) -> AsyncIterator[EchoStreamResponse]:
for _ in range(extra_times):
yield EchoStreamResponse(value=value)
async def start_server():
HOST = "127.0.0.1"
PORT = 50051
server = Server([EchoService()])
await server.start(HOST, PORT)
await server.wait_closed()
if __name__ == "__main__":
loop = asyncio.get_event_loop()
loop.run_until_complete(start_server())
loop.close()
Changes
- Added imports of
EchoReponse
andEchoStreamResponse
- Updated
echo
andecho_stream
to return the correct types - Updated
PORT
to match that in the client example - Replaced
await server.run_forever()
(raises aNotImplementedError
) withawait server.wait_closed()
- Added an asyncio loop at the bottom to actually run the server