Skip to content

Commit 56e7b98

Browse files
theunkn0wn1abn
authored andcommitted
Fix the readme gRPC usage example (danielgtaylor#122)
* re-implement README gRPC client example to be a self-contained script - fix a syntax error - fix a usage error * asyncio.run() was added in 3.7 - this lib targets >= 3.6 * Apply suggestions from code review Optimized imports, store RPC call result before printing Co-authored-by: Arun Babu Neelicattu <arun.neelicattu@gmail.com> * add entry-point check to example Co-authored-by: Arun Babu Neelicattu <arun.neelicattu@gmail.com>
1 parent e192c70 commit 56e7b98

File tree

1 file changed

+27
-13
lines changed

1 file changed

+27
-13
lines changed

README.md

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ This project exists because I am unhappy with the state of the official Google p
3737
- Uses `SerializeToString()` rather than the built-in `__bytes__()`
3838
- Special wrapped types don't use Python's `None`
3939
- Timestamp/duration types don't use Python's built-in `datetime` module
40-
4140
This project is a reimplementation from the ground up focused on idiomatic modern Python to help fix some of the above. While it may not be a 1:1 drop-in replacement due to changed method names and call patterns, the wire format is identical.
4241

4342
## Installation
@@ -126,7 +125,7 @@ Greeting(message="Hey!")
126125

127126
The generated Protobuf `Message` classes are compatible with [grpclib](https://github.com/vmagamedov/grpclib) so you are free to use it if you like. That said, this project also includes support for async gRPC stub generation with better static type checking and code completion support. It is enabled by default.
128127

129-
Given an example like:
128+
Given an example service definition:
130129

131130
```protobuf
132131
syntax = "proto3";
@@ -153,22 +152,37 @@ service Echo {
153152
}
154153
```
155154

156-
You can use it like so (enable async in the interactive shell first):
157-
155+
A client can be implemented as follows:
158156
```python
159-
>>> import echo
160-
>>> from grpclib.client import Channel
157+
import asyncio
158+
import echo
159+
160+
from grpclib.client import Channel
161161

162-
>>> channel = Channel(host="127.0.0.1", port=1234)
163-
>>> service = echo.EchoStub(channel)
164-
>>> await service.echo(value="hello", extra_times=1)
165-
EchoResponse(values=["hello", "hello"])
166162

167-
>>> async for response in service.echo_stream(value="hello", extra_times=1)
163+
async def main():
164+
channel = Channel(host="127.0.0.1", port=50051)
165+
service = echo.EchoStub(channel)
166+
response = await service.echo(value="hello", extra_times=1)
167+
print(response)
168+
169+
async for response in service.echo_stream(value="hello", extra_times=1):
168170
print(response)
169171

170-
EchoStreamResponse(value="hello")
171-
EchoStreamResponse(value="hello")
172+
# don't forget to close the channel when done!
173+
channel.close()
174+
175+
176+
if __name__ == "__main__":
177+
loop = asyncio.get_event_loop()
178+
loop.run_until_complete(main())
179+
180+
```
181+
which would output
182+
```python
183+
EchoResponse(values=['hello', 'hello'])
184+
EchoStreamResponse(value='hello')
185+
EchoStreamResponse(value='hello')
172186
```
173187

174188
### JSON

0 commit comments

Comments
 (0)