added documentation for server-facing stubs (#186)
This commit is contained in:
30
README.md
30
README.md
@@ -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:
|
||||
|
||||
Reference in New Issue
Block a user