python-betterproto icon indicating copy to clipboard operation
python-betterproto copied to clipboard

Documentation errors

Open badge opened this issue 4 years ago • 1 comments

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 and EchoStreamResponse
  • Updated echo and echo_stream to return the correct types
  • Updated PORT to match that in the client example
  • Replaced await server.run_forever() (raises a NotImplementedError) with await server.wait_closed()
  • Added an asyncio loop at the bottom to actually run the server

badge avatar Feb 04 '21 13:02 badge

I think a PR for this seems like a good idea.

Gobot1234 avatar Feb 06 '21 01:02 Gobot1234