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
3 changed files with 62 additions and 2 deletions

View File

@@ -192,6 +192,36 @@ 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
Both serializing and parsing are supported to/from JSON and Python dictionaries using the following methods: