-
Notifications
You must be signed in to change notification settings - Fork 233
Fix the readme gRPC usage example #122
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
6bcc37b
66bdec5
c4e854e
04211a2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -37,7 +37,6 @@ This project exists because I am unhappy with the state of the official Google p | |||||||||||||||
| - Uses `SerializeToString()` rather than the built-in `__bytes__()` | ||||||||||||||||
| - Special wrapped types don't use Python's `None` | ||||||||||||||||
| - Timestamp/duration types don't use Python's built-in `datetime` module | ||||||||||||||||
|
|
||||||||||||||||
| 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. | ||||||||||||||||
|
|
||||||||||||||||
| ## Installation | ||||||||||||||||
|
|
@@ -126,7 +125,7 @@ Greeting(message="Hey!") | |||||||||||||||
|
|
||||||||||||||||
| 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. | ||||||||||||||||
|
|
||||||||||||||||
| Given an example like: | ||||||||||||||||
| Given an example service definition: | ||||||||||||||||
|
|
||||||||||||||||
| ```protobuf | ||||||||||||||||
| syntax = "proto3"; | ||||||||||||||||
|
|
@@ -153,22 +152,33 @@ service Echo { | |||||||||||||||
| } | ||||||||||||||||
| ``` | ||||||||||||||||
|
|
||||||||||||||||
| You can use it like so (enable async in the interactive shell first): | ||||||||||||||||
|
|
||||||||||||||||
| A client can be implemented as follows: | ||||||||||||||||
| ```python | ||||||||||||||||
| >>> import echo | ||||||||||||||||
| >>> from grpclib.client import Channel | ||||||||||||||||
| import echo | ||||||||||||||||
| from grpclib.client import Channel | ||||||||||||||||
| import asyncio | ||||||||||||||||
|
|
||||||||||||||||
| >>> channel = Channel(host="127.0.0.1", port=1234) | ||||||||||||||||
| >>> service = echo.EchoStub(channel) | ||||||||||||||||
| >>> await service.echo(value="hello", extra_times=1) | ||||||||||||||||
| EchoResponse(values=["hello", "hello"]) | ||||||||||||||||
|
|
||||||||||||||||
| >>> async for response in service.echo_stream(value="hello", extra_times=1) | ||||||||||||||||
| async def main(): | ||||||||||||||||
| channel = Channel(host="127.0.0.1", port=50051) | ||||||||||||||||
| service = echo.EchoStub(channel) | ||||||||||||||||
| print(await service.echo(value="hello", extra_times=1)) | ||||||||||||||||
|
theunkn0wn1 marked this conversation as resolved.
Outdated
|
||||||||||||||||
|
|
||||||||||||||||
| async for response in service.echo_stream(value="hello", extra_times=1): | ||||||||||||||||
| print(response) | ||||||||||||||||
|
|
||||||||||||||||
| EchoStreamResponse(value="hello") | ||||||||||||||||
| EchoStreamResponse(value="hello") | ||||||||||||||||
| # don't forget to close the channel when done! | ||||||||||||||||
| channel.close() | ||||||||||||||||
| loop = asyncio.get_event_loop() | ||||||||||||||||
| loop.run_until_complete(main()) | ||||||||||||||||
|
|
||||||||||||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i specifically didn't use Moving the runtime into a entry point check, however, is a good idea. |
||||||||||||||||
| ``` | ||||||||||||||||
|
|
||||||||||||||||
| which would output | ||||||||||||||||
| ```python | ||||||||||||||||
| EchoResponse(values=['hello', 'hello']) | ||||||||||||||||
| EchoStreamResponse(value='hello') | ||||||||||||||||
| EchoStreamResponse(value='hello') | ||||||||||||||||
| ``` | ||||||||||||||||
|
|
||||||||||||||||
| ### JSON | ||||||||||||||||
|
|
||||||||||||||||
Uh oh!
There was an error while loading. Please reload this page.