当前位置: 首页 > news >正文

GRPC 学习记录

GRPC 安装

安装 grpcio、grpcio-tools、protobuf、

pip install grpcio -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install grpcio-tools -i https://pypi.tuna.tsinghua.edu.cn/simple
pip install protobuf -i https://pypi.tuna.tsinghua.edu.cn/simple

常用类型

在这里插入图片描述

proto 文件

// 选择 proto3
syntax = "proto3";// 名字
package test;// 服务{函数}
service Bilili {rpc Hello(HelloReq) returns (HelloReply) {}  // 双向非流// rpc Hello(stream HelloReq) returns (stream HelloReply) {}  // stream 保持链接
}// 双向非流 参数定义
// 输入函数
message HelloReq {string name = 1;int32 age = 2;repeated string data = 3;map<string, demo1> number = 4;
}message demo1 {string n1 = 1;int64 n2 = 2;bool n3 = 3;
}// 返回函数
message HelloReply {string result = 1;
}

proto 文件 转换为 python 命令

我的文件名:test.proto

python -m grpc_tools.protoc -I. --python_out=. --pyi_out=. --grpc_python_out=. test.proto

客户端

# client.py
import grpc
import test_pb2_grpc as pb2_grpc
import test_pb2 as pb2def run():# 绑定地址conn = grpc.insecure_channel('127.0.0.1:5001')# 绑定对应服务client = pb2_grpc.BililiStub(channel=conn)# 绑定服务对应的函数response = client.Hello(pb2.HelloReq(name='SB',age=33,data=['1', '2', '3'],number={'sb': pb2.demo1(n1='q', n2=33, n3=True)}), )print(response.result)if __name__ == '__main__':run()

服务端

# service.py
import time
import grpc
import test_pb2 as pb2
import test_pb2_grpc as pb2_grpc
from concurrent import futuresclass Bili(pb2_grpc.BililiServicer):def Hello(self, request, context):name = request.nameage = request.agedata = request.datamap_1 = request.numberresult = f'姓名: {name}, 年龄: {age} 数组:{data} 字典:{map_1}'return pb2.HelloReply(result=result)def run():# 服务grpc_server = grpc.server(# 设置了4个进程futures.ThreadPoolExecutor(max_workers=4),)# 注册服务pb2_grpc.add_BililiServicer_to_server(Bili(), grpc_server)# 绑定 地址grpc_server.add_insecure_port('0.0.0.0:5001')print('server start..')grpc_server.start()try:while True:time.sleep(3600)# 按Ctrl + cexcept KeyboardInterrupt:# 安全退出grpc_server.stop(0)if __name__ == '__main__':run()

在这里插入图片描述

http://www.lryc.cn/news/130679.html

相关文章:

  • C++语言的QT写软件界面,结合python深度学习模型的综合应用处理方案
  • Linux环境下python连接Oracle教程
  • 第 7 章 排序算法(1)
  • wsl,字体乱码问题
  • 【NetCore】10-路由定义
  • 软考:中级软件设计师:数据库模式、ER模型
  • 海量数据迁移,亚马逊云科技云数据库服务为大库治理提供新思路
  • DevOps系列文章之 GitlabCICD自动化部署SpringBoot项目
  • 汽车租赁管理系统/汽车租赁网站的设计与实现
  • 语句覆盖、条件覆盖、判定覆盖、条件-判定覆盖、路径覆盖
  • 二进制逻辑运算符
  • Bug日记-webstorm运行yarn 命令报错
  • C++11并发与多线程笔记(9) async、future、packaged_task、promise
  • Mr. Cappuccino的第63杯咖啡——Spring之AnnotationConfigApplicationContext源码分析
  • opencv直方图与模板匹配
  • Apache Doris 入门教程31:计算节点
  • Nacos和GateWay路由转发NotFoundException: 503 SERVICE_UNAVAILABLE “Unable to find
  • 2021年9月全国计算机等级考试真题(二级C语言)
  • 串口通讯
  • 自动拉取 GitHub 仓库更新的脚本
  • 如何获得Android 14复活节彩蛋
  • 国产32位单片机XL32F001,带1 路 12bit ADC,I2C、SPI、USART 等外设
  • typescript基础之null和undefined
  • php_mb_strlen指定扩展
  • 利用OpenCV光流算法实现视频特征点跟踪
  • 探索无限创造力的星辰大道,画出想象的浩瀚宇宙!-turtle
  • 企业数字化转型大数据湖一体化平台项目建设方案PPT
  • 【3Ds Max】车削命令的简单使用(以制作花瓶为例)
  • Python 3 使用HBase 总结
  • Maven方式构建SpringBoot项目