快快樂樂 (?) ⽤用 GRPC
+ Python
Andy Dai

daikeren@gmail.com
!1
緣起
• ⼀一切都是意外

• 60+ RD

• 4x Microservice

• ASP.net + Go + Node.js (+ Python)

• gRPC
!2
gRPC 是啥?
https://grpc.io/
!3
特⾊色
• Google 出的

• ⽀支援很多語⾔言

• 好像很厲害
!4
有個架構叫做 REST
• 各家都在⽤用

• ⽀支援很多語⾔言

• 好像沒有很厲害,可是很好理理解
!5
Problem in REST
• (也許)RESTful 不是這麼適合做 API 

• json 的問題

• Schema 

• Type

• Performance
!6
GRPC with Python in
10 minutes
!7
Protocol Buffer
• Serialization & Deserialization of Data

• Data Type

• Smaller, Faster
!8
Protocol Buffer
!9
syntax = "proto3";
package hello;
message Request {
string name = 1;
int64 val = 2;
}
enum Setting {
TEST = 0 ;
TEST2 = 1 ;
}
來來看看 Code
!10
GRPC
• Google 做的 RPC implementation

• HTTP 2.0 + Protocol Buffer 

• ⽀支援不同語⾔言的 Server/Client 實作
!11
GRPC
!12
Why GRPC?
• 你需要建構 Low Latency 的系統

• 你要建構 REST 可能沒辦法滿⾜足需求的系統

• Mobile Client

• 你的研發團隊使⽤用了了不同語⾔言建構 microservice
!13
4 kinds of RPC
• Unary RPC

• Server streaming RPC

• Client streaming RPC

• Bidirectional streaming RPC
!14
開發 GRPC 程式
• 安裝套件

• 寫 .proto 檔案

• Implement Server 

• Implement Client
!15
安裝套件
!16
pip install grpcio grpcio-tools
.proto File
!17
syntax = "proto3";
package hello;
message Request {
string name = 1;
}
message Response {
string val = 1;
}
service Hello {
rpc Echo(Request) returns (Response);
}
codegen
!18
python -m grpc_tools.protoc 

--proto_path=. 

--python_out=. 

--grpc_python_out=. 

hello.proto
proto_path: .proto 檔案的位置
python_out: protobuf Python code 的位置
grpc_python_out: gRPC Python code 的位置
來來看看 Code
!19
GRPC in Python - The bad
parts
• Performance 

• ECO-system

• gRPC + Python 的⼩小問題
!20
Conclusion
• 對於 GRPC with Python 的簡單介紹

• protobuf

• Server/Client Implementation

• 該⽤用 GRPC 嗎?
!21
Q&A
!22
Reference
• https://grpc.io/

• gRPC errors http://avi.im/grpc-errors/

• https://github.com/daikeren/grpc_talk
!23

Grpc + python.key