脚本之家,脚本语言编程技术及教程分享平台!
分类导航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服务器之家 - 脚本之家 - Python - python下grpc与protobuf的编写使用示例

python下grpc与protobuf的编写使用示例

2022-11-27 13:33Jeff的技术栈 Python

这篇文章主要为大家介绍了python下grpc与protobuf的编写使用,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步早日升职加薪

1. python下protobuf使用

1.1 安装protobuf

?
1
2
pip3.6 install grpcio #安装grpc
pip3.6 install grpcio-tools #安装grpc tools

1.2 protobuf3定义格式

新建protobuf文件名:hello.proto

?
1
2
3
4
syntax = "proto3";
message HelloRequest {
  string name = 1;
}

1.3 生成proto的python文件

?
1
2
3
4
5
6
7
8
cd hello.proto文件路径下
命令:python3.6 -m grpc_tools.protoc --python_out=. --grpc_python_out=. -I. hello.proto
注意:只有python生成两个文件
命令解释:
python3.6 -m grpc_tools.protoc:实际需要使用grpc_tools.protoc这里面的命令
--python_out=. :生成的python文件放在当前路径下,这是给rpc用的文件
--grpc_python_out=. :生成的python文件放在当前路径下,这是给grpc用的文件
-I.:指import,当前目录下找hello.proto文件

python下grpc与protobuf的编写使用示例

1.4 对比一下protobuf生成的效果

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
res.SerializeToString() # 序列化二进制
res2.ParseFromString(res_str) # 反序列化二进制
import json
from python_grpc.proto import hello_pb2
def main():
    res = hello_pb2.HelloRequest()
    res.name = "jeff"
    res_str = res.SerializeToString() # 序列化二进制
    print(res_str) # b'\n\x04jeff'
    print(len((res_str))) # 6,和json对比,josn长度为:16
    res2 = hello_pb2.HelloRequest()
    res2.ParseFromString(res_str) # 反序列化二进制
    print(res2.name) # jeff
    res_json = {
        "name":"jeff"
    }
    print(len(json.dumps(res_json))) # 16,json和proto压缩对比,proto压缩后:6
if __name__ == '__main__':
    main()

2.python下grpc使用

2.1编写hello.proto文件

?
1
2
3
4
5
6
7
8
9
10
11
12
13
syntax = "proto3";
package services;
option go_package = "./;proto";
service Greeter {
    // 定义SayHello方法
    rpc SayHello (HelloRequest) returns (HelloReply);
}
message HelloRequest {
    string name = 1; //编号
}
message HelloReply {
    string message = 1; //编号
}

2.2 生成proto的python文件

?
1
2
3
4
5
6
7
8
9
10
11
cd hello.proto文件路径下
命令:python3.6 -m grpc_tools.protoc --python_out=. --grpc_python_out=. -I. hello.proto
注意:只有python生成两个文件
命令解释:
python3.6 -m grpc_tools.protoc:实际需要使用grpc_tools.protoc这里面的命令
--python_out=. :生成的python文件放在当前路径下,这是给rpc用的文件
--grpc_python_out=. :生成的python文件放在当前路径下,这是给grpc用的文件
-I.:指import,当前目录下找hello.proto文件
注意:生成的*_grpc.py文件的导包需要修改,否则报错:要让python找到hello_pb2
import hello_pb2 as hello__pb2 改为:
from python_grpc.proto import hello_pb2 as hello__pb2

2.3 编写server端

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
from concurrent import futures
import grpc
from python_grpc.proto import hello_pb2, hello_pb2_grpc
# 业务处理
class Greeter(hello_pb2_grpc.GreeterServicer):
    def SayHello(self, request, context):
        return hello_pb2.HelloReply(message=f"你好,{request.name}")
# 启动
def start():
    # 1.实例化server
    Thread = futures.ThreadPoolExecutor(max_workers=10## 设置线程池,并发大小
    server = grpc.server(Thread)
    # 2.注册逻辑到server中
    hello_pb2_grpc.add_GreeterServicer_to_server(Greeter(), server)
    # 3.启动server
    server.add_insecure_port("127.0.0.1:8888")
    server.start()
    server.wait_for_termination()
if __name__ == '__main__':
    start()

2.4 编写cilent端

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
import grpc
from python_grpc.proto import hello_pb2, hello_pb2_grpc
# rpc调用
def main():
    # 这里使用with,调用完会自动关闭。优雅写法
    with grpc.insecure_channel("127.0.0.1:8888") as channel:
        stub = hello_pb2_grpc.GreeterStub(channel)
        # 调用定义的SayHello方法
        rep = stub.SayHello(
            hello_pb2.HelloRequest(name="jeff"# 传递定义的HelloRequest类型参数
        )
        return rep
# 业务代码
def start():
    rep = main()
    print(rep.message)  # 你好,jeff
if __name__ == '__main__':
    start()

以上就是python下grpc与protobuf的编写使用的详细内容,更多关于python grpc与protobuf编写使用的资料请关注服务器之家其它相关文章!

原文链接:https://www.cnblogs.com/guyouyin123/p/16133065.html

延伸 · 阅读

精彩推荐