python/c++ 利用gRPC完成python远程调用c++

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) {
     }
}
//输入参数
// The request message containing the user's name.
message HelloRequest {
     
  string name = 1;
}
//输出参数
// The response message containing the greetings
message HelloReply {
     
  string message = 1;
}

生成接口文件:helloworld_pb2.py,helloworld_pb2_grpc.py

#-I:运行目录路径名
#--python_out:生成Python源文件
#--grpc_out:生成grpc源文件
#--plugin:指定要使用的插件可执行文件。这里是Python路径
#.是当前目录  ..是当前目录的上层目录
$ protoc -I ../../protos --python_out=. --grpc_out=. --plugin=protoc-gen-grpc='which grpc_python_plugin' ../../protos/route_guide.proto

sercer.py

from concurrent import futures
import time
import logging

import grpc

import helloworld_pb2
import helloworld_pb2_grpc

_ONE_DAY_IN_SECONDS = 60 * 60 * 24

#Greeter:服务名;SayHello:方法名
class Greeter(helloworld_pb2_grpc.GreeterServicer):

    def SayHello(self, request, context):
    	#请求内容在request中,取数据:request.name
        return helloworld_pb2.HelloReply(message='Hello, %s!' % request.name)


def serve():
	#max_workers:最大连接数
    server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
    helloworld_pb2_grpc.add_GreeterServicer_to_server(Greeter(), server)
    #服务启动的ip和端口
    server.add_insecure_port('[::]:50051')
    server.start()
    try:
        while True:
            time.sleep(_ONE_DAY_IN_SECONDS)
    except KeyboardInterrupt:
        server.stop(0)


if __name__ == '__main__':
    logging.basicConfig()
    serve()

client.py

from __future__ import print_function
import logging

import grpc

import helloworld_pb2
import helloworld_pb2_grpc


def run():
	#请求的ip和端口地址
    with grpc.insecure_channel('localhost:50051') as channel:
        stub = helloworld_pb2_grpc.GreeterStub(channel)
        #SayHello:请求方法;name:请求参数
        response = stub.SayHello(helloworld_pb2.HelloRequest(name='you'))
    print("Greeter client received: " + response.message)


if __name__ == '__main__':
    logging.basicConfig()
    run()

参考
gRPC官方文档中文版_V1.0
http://doc.oschina.net/grpc?t=60138
Python:如何将proto文件编译为python文件
https://blog.csdn.net/Wendy_LWZ/article/details/80654703
Python:GRPC接口编写之如何编写服务端与客户端
https://blog.csdn.net/Wendy_LWZ/article/details/81330799
gRPC 在Python中的应
https://blog.csdn.net/alanguoo/article/details/78635260

你可能感兴趣的:(python,c++,python,grpc)