This is a repo created for an internal presentation to demonstrate some high level concepts of gRPC in Python.
This repo contains:
- An account protobuf
- A Python gRPC server implementation of
account.proto - A Python gRPC stub (client) implementation of
account.proto
We implement two RPC methods to demonstrate:
GetAccount- a unary connection (like a REST GET request/response)StreamIndustryIds- a client-side streaming connection (streams responses as an iterator from server)
There are three branches:
main- original implementation
01-update-server-with-new-fields- add a new field to
GetAccountResponseand update server to return the new field - client should continue to work
- add a new field to
02-update-client-to-use-new-contract- update client to implement new contract
Use your favourite tool to create virtual environment and install dependencies in requirements.txt.
In ./account_server/account_server and ./account_client/account_client, use the following command to generate three files:
account_pb2.py- contains the generated message types and enums defined in the protobuf file
account_pb2_grpc.py- contains the generated service servicer (server) and stub (stub) abstract classes to be implemented
account_pb2.pyi- contains mypy types using the plugin
mypy_protobuf
- contains mypy types using the plugin
python -m grpc_tools.protoc \
-I ../../protos \
--plugin=protoc-en-mypy=/Users/alanlau/.pyenv/shims/protoc-gen-mypy \
--mypy_out=. \
--python_out=. \
--grpc_python_out=. \
../../protos/account.protoRerun this step if you've made changes to the protobuf account.proto.
The server code lives under ./account_server, and the stub (client) lives under ./account_client.
They can be updated independently of each other in the case of new field or features added in contract.
python account_[server|client].py