gRPC repeated数组的使用 python
reco.proto
syntax = "proto3";
package rpc_package;
service HelloWorldService {
rpc SayHello (HelloRequest) returns (HelloReply) {}
}
message HelloRequest {
repeated Student student=1;
}
message Student{
int32 age=1;
string name=2;
}
message HelloReply {
string message = 1;
repeated int32 s_list=2;
}

python -m grpc_tools.protoc -I. --python_out=. --grpc_python_out=. reco.proto
创建出 reco_pb2.py 和 reco_pb2_grpc.py 两个文件
然后写服务端代码
test_grpc_server.py
from concurrent import futures
import grpc
import logging
import time from reco_pb2_grpc import add_HelloWorldServiceServicer_to_server, \
HelloWorldServiceServicer
from reco_pb2 import HelloRequest, HelloReply class Hello(HelloWorldServiceServicer): # 这里实现我们定义的接口
def SayHello(self, request, context):
s_list = [0,1,2,4,3]
return HelloReply(message='Hello, {}! '.format(request.student),s_list=s_list) def serve():
# 这里通过thread pool来并发处理server的任务
server = grpc.server(futures.ThreadPoolExecutor(max_workers=10)) # 将对应的任务处理函数添加到rpc server中
add_HelloWorldServiceServicer_to_server(Hello(), server) # 这里使用的非安全接口,世界gRPC支持TLS/SSL安全连接,以及各种鉴权机制
server.add_insecure_port('[::]:50000')
server.start()
try:
while True:
time.sleep(60 * 60 * 24)
except KeyboardInterrupt:
server.stop(0) if __name__ == "__main__":
logging.basicConfig()
serve()
test_grpc_client.py
from __future__ import print_function
import logging import grpc
from reco_pb2 import HelloRequest, HelloReply
from reco_pb2_grpc import HelloWorldServiceStub def run():
# 使用with语法保证channel自动close
with grpc.insecure_channel('localhost:50000') as channel:
# 客户端通过stub来实现rpc通信
stub = HelloWorldServiceStub(channel) # 客户端必须使用定义好的类型,这里是HelloRequest类型
hellorequest = HelloRequest()
for i in range(5):
request = hellorequest.student.add()
request.age = 18
request.name = "xinyuuliu" response = stub.SayHello(hellorequest)
print ("hello client received: " + response.message)
print(response.s_list) if __name__ == "__main__":
logging.basicConfig()
run()
结果:
hello client received: Hello, [age: 18
name: "xinyuuliu"
, age: 18
name: "xinyuuliu"
, age: 18
name: "xinyuuliu"
, age: 18
name: "xinyuuliu"
, age: 18
name: "xinyuuliu"
]!
[0, 1, 2, 4, 3]
gRPC repeated数组的使用 python的更多相关文章
- 按固定元素数目分割数组- perl,python
要求:把40个元素的数组,按每行8个,分5行打印出来.如下图 1 2 3 4 5 6 7 89 10 11 12 13 14 15 1617 18 19 20 21 22 23 2425 26 27 ...
- 打印数组所有排列 python
本人.net一名,最近在看数据结构与算法分析,中间涉及的一些比较有意思的算法题,打算用python实现以下.选择python的原因,就是想熟悉一下python的语法,和pycharm基本的应用. 本篇 ...
- Python与数据结构[1] -> 栈/Stack[0] -> 链表栈与数组栈的 Python 实现
栈 / Stack 目录 链表栈 数组栈 栈是一种基本的线性数据结构(先入后出FILO),在 C 语言中有链表和数组两种实现方式,下面用 Python 对这两种栈进行实现. 1 链表栈 链表栈是以单链 ...
- Python与数据结构[2] -> 队列/Queue[0] -> 数组队列的 Python 实现
队列 / Queue 数组队列 数组队列是队列基于数组的一种实现,其实现类似于数组栈,是一种FIFO的线性数据结构. Queue: <--| 1 | 2 | 3 | 4 | 5 |<-- ...
- 合并2个数组为1个无重复元素的有序数组--Go对比Python
Go实现: 1 package main 2 3 import ( 4 "fmt" 5 "sort" 6 ) 7 8 func main() { 9 var a ...
- Leetcode OJ : Repeated DNA Sequences hash python solution
Total Accepted: 3790 Total Submissions: 21072 All DNA is composed of a series of nucleotides abb ...
- leetcode NO.349 两个数组的交集 (python实现)
来源 https://leetcode-cn.com/problems/intersection-of-two-arrays/ 题目描述 给定两个数组,写一个函数来计算它们的交集. 例子: 给定 nu ...
- leetcode 238. 除自身以外数组的乘积 (python)
给定长度为 n 的整数数组 nums,其中 n > 1,返回输出数组 output ,其中 output[i] 等于 nums 中除 nums[i] 之外其余各元素的乘积. 示例: 输入: [1 ...
- 后缀数组【原理+python代码】
后缀数组 参考:https://blog.csdn.net/a1035719430/article/details/80217267 https://blog.csdn.net/YxuanwKeith ...
- 剑指offer-字符串的排列-数组-递归-动态规划-python
题目描述 输入一个字符串,按字典序打印出该字符串中字符的所有排列.例如输入字符串abc,则打印出由字符a,b,c所能排列出来的所有字符串abc,acb,bac,bca,cab和cba. 输入描述: 输 ...
随机推荐
- wsl使用记录
# wsl使用记录 安装 直接参考微软官方文档使用 WSL 在 Windows 上安装 Linux ubuntu可用发行版安装 https://ubuntu.com/wsl 访问 方式一:在资源管理器 ...
- Zabbix与乐维监控对比分析(五)——可视化篇
前面我们详细介绍了Zabbix与乐维监控的架构与性能.Agent管理.自动发现.权限管理.对象管理.告警管理方面的对比分析,相信大家对二者的对比分析有了相对深入的了解,接下来我们将对二者的可视化功能进 ...
- 使用 libreoffice 批量化转化文件为 .pdf 并合并
介绍使用 libreoffice 批量化将文件转化为 .pdf 然后合并.pdf文件的方法 很多人知道,在 Linux 系统中 WPS 是办公软件中很棒的选择.但其实 libreoffice 也是一个 ...
- 基于python源码的啸叫抑制算法解析
一 原理解析 从下图一中可以看出,该算法的原理也是先检测出来啸叫,然后通过陷波器来进行啸叫抑制的,和笔者以前分析的所用方法基本耦合. 二 源码分析 函数PAPR:计算峰值功率和平均功率的比 ...
- Codeforces Round 858:B. Mex Master
一.来源:Problem - B - Codeforces 二.题面 三.思路 题面:n个非负正数,随机排列并由相邻两个数相加构成n-1个数并进行升序排列,求从0开始的第一个MEX(Minimum E ...
- ERROR 1290 (HY000): The MySQL server is running with the --skip-grant-tables option so it cannot execute this statement
先执行: flush privileges; 再执行修改密码命令,可以了: set password for root@localhost=password('你的密码');
- 【K8S】Docker向私有仓库拉取/推送镜像报错(http: server gave HTTP response to HTTPS client)
这里,我们搭建的Harbor仓库的地址为 http://192.168.175.101:1180. 报错信息如下所示. [root@binghe101 ~]# docker login 192.168 ...
- C# OpenCvSharp-HoughCircles(霍夫圆检测) 简单计数
效果 项目 代码 using System; using System.Collections.Generic; using System.ComponentModel; using System.D ...
- ABC327 A-F
vp on 2023.11.12 A: 使用 string 的 find,find 在找不到时会返回string::npos. B.C:略. D 发现原题相当于二分图判定. E 线性DP. F 原题相 ...
- 【已解决】IDEA项目启动时Maven报错:Downgrade Maven to version 3.8.1 or earlier in settings
报错场景如下: Possible solutions: Check that Maven pom files not contain http repository http://mvn.gt.ige ...