您现在的位置是:网站首页> 编程资料编程资料
python下grpc与protobuf的编写使用示例_python_
2023-05-26
399人已围观
简介 python下grpc与protobuf的编写使用示例_python_
1. python下protobuf使用
1.1 安装protobuf
pip3.6 install grpcio #安装grpc pip3.6 install grpcio-tools #安装grpc tools
1.2 protobuf3定义格式
新建protobuf文件名:hello.proto
syntax = "proto3"; message HelloRequest { string name = 1; } 1.3 生成proto的python文件
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文件

1.4 对比一下protobuf生成的效果
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文件
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文件
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端
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端
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编写使用的资料请关注其它相关文章!
您可能感兴趣的文章:
相关内容
- python文件操作的基础详细讲解(write、read、readlines、readline)_python_
- Python colorama 彩色打印实现代码_python_
- GitHub AI编程工具copilot在Pycharm的应用_python_
- 简单聊聊Python中的鸭子类型和猴子补丁_python_
- python 包之 re 正则匹配教程分享_python_
- python 包之 Pillow 图像处理教程分享_python_
- Python数据传输黏包问题_python_
- Python爬虫之网络请求_python_
- Python异步爬取知乎热榜实例分享_python_
- Python爬取城市租房信息实战分享_python_
