Python gRPC

For revisit, go to check the Python Basic tutorial and Sample Code.

The gRPC is the open source version of Stubby from Google, it uses Protocol Buffer as both its Interface Definition Language (IDL) and as its underlying message interchange format.

gRPC clients and servers can run and talk to each other in a variety of environments and can be written in any of gRPC’s supported languages. So, for example, you can easily create a gRPC server in Java with clients in Go, Python, or Ruby.

Core Concepts for gRPC. There are regular and stream request/response types, they can be combined in any form, for example:

1
2
3
4
5
6
7
8
service Greeter {
rpc SayHello(HelloRequest) returns (HelloResponse) {}
// Response is using yield.
rpc LotsOfReplies(HelloRequest) returns (stream HelloResponse) {}
// Request is using iterator.
rpc ZLotsOfGreetings(stream HelloRequest) returns (HelloResponse) {}
rpc BidiHello(stream HelloRequest) returns (stream HelloResponse) {}
}

The comments in proto service definition will be translated to docstring in generated code, for example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// import definition.
// https://developers.google.com/protocol-buffers/docs/proto#importing
import "xxx/utility.proto";

// The greeting service definition.
service Greeter {
// Sends a greeting.
rpc SayHello (HelloRequest) returns (HelloReply) {}
// Sends a greeting again.
rpc SayHelloAgain (HelloRequest) returns (HelloReply) {}
}

// The request message containing the user's name.
message HelloRequest {
string name = 1;
}
// The response message containing the greetings
message HelloReply {
string message = 1;
}

As above shows, write message and service in .proto file.

Basic tutorial for Python, run within the python virtualenv, for example:

1
2
3
virtualenv -p python3 grpc
# python venv manager if you have installed.
workon grpc

Required python modules for gRPC:

1
2
3
4
5
6
7
# Install gRPC.
python -m pip install grpcio

# Python’s gRPC tools include the protocol buffer compiler protoc and the
# special plugin for generating server and client code from .proto service
# definitions.
python -m pip install grpcio-tools

After finished the proto file definition:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# Command help
python -m grpc_tools.protoc --help

# This will generate xx_pb2.py and xx_pb2_grpc.py file.
# xx_pb2.py: Contains request and response message classes.
# xx_pb2_grpc.py: Contains client and server and utilities classes:
# - class xxxStub(object)
# - class xxxServicer(object)
# - def add_xxxServicer_to_server(servicer, server)
# The relative path can be used here.
python -m grpc_tools.protoc \
--proto_path=<target proto file folder path> \
--python_out=<Generate Python source file> \
--pyi_out=<Generate Python pyi stub> \
--grpc_python_out=<Generate Python source file> \
<target proto file path>

When finishes the basic codelab, here are many different patterns and usages to create client-server applications.

TIPS: Using Python typing to indicate the parameters type in service method to make it clear.

NOTE: In basic tutorial for asynchronous pattern, it uses Python asyncio as implementation.

For gRPC authentication in Python, please see ALTS authentication.

0%