forked from pantsbuild/pants
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
documentation for grpcio (pantsbuild#7155)
### Problem Documentation for grpcio generation tool
- Loading branch information
1 parent
f73f112
commit b2f5a49
Showing
3 changed files
with
58 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
# Python gRPC + protobufs | ||
|
||
## Problem | ||
You have `.proto` files defining protobufs and grpc services and you want Pants to generate Python code from them that you can use from a Python application. | ||
|
||
## Solution | ||
Create `python_grpc_library` targets and use the gen goal to generate code from the `.proto` files. There is a codegen task grpcio-run, that uses Python's grpcio library https://grpc.io/ and generates python code from .proto files. | ||
|
||
## Usage | ||
|
||
in a `BUILD` file near your proto files, create a `python_grpcio_library` target with your protos as a `sources`. | ||
|
||
```build | ||
python_grpcio_library( | ||
sources=['service.proto'], | ||
dependencies=[ | ||
'3rdparty/python:protobuf', | ||
] | ||
) | ||
``` | ||
|
||
Then, you can add a dependency on this target in your python binary's `BUILD` file `dependencies` section: | ||
|
||
```build | ||
python_binary( | ||
source='server.py', | ||
dependencies=[ | ||
# [...] | ||
'examples/src/protobuf/org/pantsbuild/example/grpcio/service' | ||
], | ||
) | ||
``` | ||
|
||
## Example: | ||
An example Python grpc client/server can be found in [/examples/src/python/example/grpcio](https://github.com/pantsbuild/pants/tree/master/examples/src/python/example/grpcio) | ||
|
||
to create a gRPC server execute | ||
```bash | ||
./pants run examples/src/python/example/grpcio/server | ||
``` | ||
|
||
and when server is running, run client example: | ||
```bash | ||
./pants run examples/src/python/example/grpcio/client | ||
``` | ||
|
||
generated code can be found as usual in pants output directory: | ||
```bash | ||
./pants.d/gen/grpcio-run/current/examples.src.protobuf.org.pantsbuild.example.service.service/current/org/pantsbuild/example/service | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters