Python Django写的后台,想要调用小伙伴的c++模块方法,目前想到三种解决方案:http,gRPC,本地调用(ctypes,SWIG)
出于以下考虑:
- 后台与模型模块分离部署(部分模型模块对GPU,显存等有各类要求)
- 跨语言(Python 调用 c++方法)
- 简单,学习时长短
决定学习并尝试用gRPC完成此次调用。
步骤
- 编写.proto文件定义服务,规定接口样式。
- 通过.proto文件的服务定义中生成gRPC客户端和服务器端的接口(python生成***_pb2.py和***_pb2_grpc.py),可以通过protocol buffer的编译器protoc以及一个特殊的gRPC Python插件来完成(不同语言插件、包不同)
- 使用gRPC的API完成不同语言的client和server
安装
#gRPC 的安装:
$ pip install grpcio
#安装 ProtoBuf 相关的 python 依赖库:
$ pip install protobuf
#安装 python grpc 的 protobuf 编译工具:
$ pip install grpcio-tools
grpc git examples(python)
helloworld.proto
// Filename: helloworld.proto
syntax="proto3";
package helloworld;
//Greeter:服务名称;SayHello:函数名;
//gRPC允许定义4种类型的service方法:
//简单RPC;应答流式RPC;请求流式RPC;双向流式RPC
service Greeter {
rpc SayHello (HelloRequest) returns (HelloReply) {
}
}
<