本文介绍了在Python中使用gRPC的方法示例,分享给大家,具体如下:
使用Protocol Buffers的跨平台RPC系统。
安装
使用 pip
pip install grpcio
pip install grpcio-tools googleapis-common-protos
gRPC由两个部分构成,grpcio 和 gRPC 工具, 后者是编译 protocol buffer 以及提供生成代码的插件。
使用
编写protocol buffer
使用 gRPC 首先需要做的是设计 protocol buffer。新建一个 msg.proto 文件。
syntax = "proto3";
service MsgService {
rpc GetMsg (MsgRequest) returns (MsgResponse){}
}
message MsgRequest {
string name = 1;
}
message MsgResponse {
string msg = 1;
}
以上面的这个消息服务为例,首先是规定语法,这里使用的是 proto3 的语法。接着使用 service 关键字定义服务,gRPC 提供4种 RPC 类型的服务,这里定义的是第一种单一请求单一回应&#