added documentation for server-facing stubs (#186)

This commit is contained in:
Tim Schmidt 2021-01-24 22:20:32 +01:00 committed by GitHub
parent 1d54ef8f99
commit 8eea5fe256
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 62 additions and 2 deletions

View File

@ -192,6 +192,36 @@ EchoStreamResponse(value='hello')
EchoStreamResponse(value='hello') EchoStreamResponse(value='hello')
``` ```
This project also produces server-facing stubs that can be used to implement a Python
gRPC server.
To use them, simply subclass the base class in the generated files and override the
service methods:
```python
from echo import EchoBase
from grpclib.server import Server
from typing import AsyncIterator
class EchoService(EchoBase):
async def echo(self, value: str, extra_times: int) -> "EchoResponse":
return value
async def echo_stream(
self, value: str, extra_times: int
) -> AsyncIterator["EchoStreamResponse"]:
for _ in range(extra_times):
yield value
async def start_server():
HOST = "127.0.0.1"
PORT = 1337
server = Server([EchoService()])
await server.start(HOST, PORT)
await server.serve_forever()
```
### JSON ### JSON
Both serializing and parsing are supported to/from JSON and Python dictionaries using the following methods: Both serializing and parsing are supported to/from JSON and Python dictionaries using the following methods:

View File

@ -12,7 +12,7 @@ Features:
- Generated messages are both binary & JSON serializable - Generated messages are both binary & JSON serializable
- Messages use relevant python types, e.g. ``Enum``, ``datetime`` and ``timedelta`` - Messages use relevant python types, e.g. ``Enum``, ``datetime`` and ``timedelta``
objects objects
- ``async``/``await`` support for gRPC Clients - ``async``/``await`` support for gRPC Clients and Servers
- Generates modern, readable, idiomatic python code - Generates modern, readable, idiomatic python code
Contents: Contents:

View File

@ -100,7 +100,7 @@ Async gRPC Support
++++++++++++++++++ ++++++++++++++++++
The generated code includes `grpclib <https://grpclib.readthedocs.io/en/latest>`_ based The generated code includes `grpclib <https://grpclib.readthedocs.io/en/latest>`_ based
stub (client) classes for rpc services declared in the input proto files. stub (client and server) classes for rpc services declared in the input proto files.
It is enabled by default. It is enabled by default.
@ -160,6 +160,36 @@ The generated client can be used like so:
EchoStreamResponse(value='hello') EchoStreamResponse(value='hello')
The server-facing stubs can be used to implement a Python
gRPC server.
To use them, simply subclass the base class in the generated files and override the
service methods:
.. code-block:: python
from echo import EchoBase
from grpclib.server import Server
from typing import AsyncIterator
class EchoService(EchoBase):
async def echo(self, value: str, extra_times: int) -> "EchoResponse":
return value
async def echo_stream(
self, value: str, extra_times: int
) -> AsyncIterator["EchoStreamResponse"]:
for _ in range(extra_times):
yield value
async def start_server():
HOST = "127.0.0.1"
PORT = 1337
server = Server([EchoService()])
await server.start(HOST, PORT)
await server.serve_forever()
JSON JSON
++++ ++++
Message objects include :meth:`betterproto.Message.to_json` and Message objects include :meth:`betterproto.Message.to_json` and