Skip to content

Commit 8eea5fe

Browse files
authored
added documentation for server-facing stubs (#186)
1 parent 1d54ef8 commit 8eea5fe

File tree

3 files changed

+62
-2
lines changed

3 files changed

+62
-2
lines changed

README.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,36 @@ EchoStreamResponse(value='hello')
192192
EchoStreamResponse(value='hello')
193193
```
194194

195+
This project also produces server-facing stubs that can be used to implement a Python
196+
gRPC server.
197+
To use them, simply subclass the base class in the generated files and override the
198+
service methods:
199+
200+
```python
201+
from echo import EchoBase
202+
from grpclib.server import Server
203+
from typing import AsyncIterator
204+
205+
206+
class EchoService(EchoBase):
207+
async def echo(self, value: str, extra_times: int) -> "EchoResponse":
208+
return value
209+
210+
async def echo_stream(
211+
self, value: str, extra_times: int
212+
) -> AsyncIterator["EchoStreamResponse"]:
213+
for _ in range(extra_times):
214+
yield value
215+
216+
217+
async def start_server():
218+
HOST = "127.0.0.1"
219+
PORT = 1337
220+
server = Server([EchoService()])
221+
await server.start(HOST, PORT)
222+
await server.serve_forever()
223+
```
224+
195225
### JSON
196226

197227
Both serializing and parsing are supported to/from JSON and Python dictionaries using the following methods:

docs/index.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ Features:
1212
- Generated messages are both binary & JSON serializable
1313
- Messages use relevant python types, e.g. ``Enum``, ``datetime`` and ``timedelta``
1414
objects
15-
- ``async``/``await`` support for gRPC Clients
15+
- ``async``/``await`` support for gRPC Clients and Servers
1616
- Generates modern, readable, idiomatic python code
1717

1818
Contents:

docs/quick-start.rst

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ Async gRPC Support
100100
++++++++++++++++++
101101

102102
The generated code includes `grpclib <https://grpclib.readthedocs.io/en/latest>`_ based
103-
stub (client) classes for rpc services declared in the input proto files.
103+
stub (client and server) classes for rpc services declared in the input proto files.
104104
It is enabled by default.
105105

106106

@@ -160,6 +160,36 @@ The generated client can be used like so:
160160
EchoStreamResponse(value='hello')
161161
162162
163+
The server-facing stubs can be used to implement a Python
164+
gRPC server.
165+
To use them, simply subclass the base class in the generated files and override the
166+
service methods:
167+
168+
.. code-block:: python
169+
170+
from echo import EchoBase
171+
from grpclib.server import Server
172+
from typing import AsyncIterator
173+
174+
175+
class EchoService(EchoBase):
176+
async def echo(self, value: str, extra_times: int) -> "EchoResponse":
177+
return value
178+
179+
async def echo_stream(
180+
self, value: str, extra_times: int
181+
) -> AsyncIterator["EchoStreamResponse"]:
182+
for _ in range(extra_times):
183+
yield value
184+
185+
186+
async def start_server():
187+
HOST = "127.0.0.1"
188+
PORT = 1337
189+
server = Server([EchoService()])
190+
await server.start(HOST, PORT)
191+
await server.serve_forever()
192+
163193
JSON
164194
++++
165195
Message objects include :meth:`betterproto.Message.to_json` and

0 commit comments

Comments
 (0)