Skip to content

Commit a331265

Browse files
authored
Fix readme docs 'Async gRPC Support' (#249)
1 parent 02e41af commit a331265

File tree

1 file changed

+18
-11
lines changed

1 file changed

+18
-11
lines changed

README.md

+18-11
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,12 @@ service Echo {
160160
}
161161
```
162162

163+
Generate echo proto file:
164+
165+
```
166+
python -m grpc_tools.protoc -I . --python_betterproto_out=. echo.proto
167+
```
168+
163169
A client can be implemented as follows:
164170
```python
165171
import asyncio
@@ -199,28 +205,29 @@ To use them, simply subclass the base class in the generated files and override
199205
service methods:
200206

201207
```python
202-
from echo import EchoBase
208+
import asyncio
209+
from echo import EchoBase, EchoResponse, EchoStreamResponse
203210
from grpclib.server import Server
204211
from typing import AsyncIterator
205212

206213

207214
class EchoService(EchoBase):
208215
async def echo(self, value: str, extra_times: int) -> "EchoResponse":
209-
return value
216+
return EchoResponse([value for _ in range(extra_times)])
210217

211-
async def echo_stream(
212-
self, value: str, extra_times: int
213-
) -> AsyncIterator["EchoStreamResponse"]:
218+
async def echo_stream(self, value: str, extra_times: int) -> AsyncIterator["EchoStreamResponse"]:
214219
for _ in range(extra_times):
215-
yield value
220+
yield EchoStreamResponse(value)
216221

217222

218-
async def start_server():
219-
HOST = "127.0.0.1"
220-
PORT = 1337
223+
async def main():
221224
server = Server([EchoService()])
222-
await server.start(HOST, PORT)
223-
await server.serve_forever()
225+
await server.start("127.0.0.1", 50051)
226+
await server.wait_closed()
227+
228+
if __name__ == '__main__':
229+
loop = asyncio.get_event_loop()
230+
loop.run_until_complete(main())
224231
```
225232

226233
### JSON

0 commit comments

Comments
 (0)