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的更多相关文章

  1. 按固定元素数目分割数组- 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 ...

  2. 打印数组所有排列 python

    本人.net一名,最近在看数据结构与算法分析,中间涉及的一些比较有意思的算法题,打算用python实现以下.选择python的原因,就是想熟悉一下python的语法,和pycharm基本的应用. 本篇 ...

  3. Python与数据结构[1] -> 栈/Stack[0] -> 链表栈与数组栈的 Python 实现

    栈 / Stack 目录 链表栈 数组栈 栈是一种基本的线性数据结构(先入后出FILO),在 C 语言中有链表和数组两种实现方式,下面用 Python 对这两种栈进行实现. 1 链表栈 链表栈是以单链 ...

  4. Python与数据结构[2] -> 队列/Queue[0] -> 数组队列的 Python 实现

    队列 / Queue 数组队列 数组队列是队列基于数组的一种实现,其实现类似于数组栈,是一种FIFO的线性数据结构. Queue: <--| 1 | 2 | 3 | 4 | 5 |<-- ...

  5. 合并2个数组为1个无重复元素的有序数组--Go对比Python

    Go实现: 1 package main 2 3 import ( 4 "fmt" 5 "sort" 6 ) 7 8 func main() { 9 var a ...

  6. Leetcode OJ : Repeated DNA Sequences hash python solution

    Total Accepted: 3790 Total Submissions: 21072     All DNA is composed of a series of nucleotides abb ...

  7. leetcode NO.349 两个数组的交集 (python实现)

    来源 https://leetcode-cn.com/problems/intersection-of-two-arrays/ 题目描述 给定两个数组,写一个函数来计算它们的交集. 例子: 给定 nu ...

  8. leetcode 238. 除自身以外数组的乘积 (python)

    给定长度为 n 的整数数组 nums,其中 n > 1,返回输出数组 output ,其中 output[i] 等于 nums 中除 nums[i] 之外其余各元素的乘积. 示例: 输入: [1 ...

  9. 后缀数组【原理+python代码】

    后缀数组 参考:https://blog.csdn.net/a1035719430/article/details/80217267 https://blog.csdn.net/YxuanwKeith ...

  10. 剑指offer-字符串的排列-数组-递归-动态规划-python

    题目描述 输入一个字符串,按字典序打印出该字符串中字符的所有排列.例如输入字符串abc,则打印出由字符a,b,c所能排列出来的所有字符串abc,acb,bac,bca,cab和cba. 输入描述: 输 ...

随机推荐

  1. iview viewdesign 里面的Select 输入添加 query不能从新为空 @on-create 解决方案 v-if 从新刷一遍

    iview viewdesign 里面的Select 输入添加 query不能从新为空 @on-create 解决方案 v-if 从新刷一遍

  2. SecureCRT windows 登录 linux

    SecureCRT是一款支持SSH(SSH1和SSH2)的终端仿真程序,简单地说是Windows下登录UNIX或Linux服务器主机的软件.SecureCRT支持SSH,同时支持Telnet和rlog ...

  3. Android热点SoftAP使用方式

    一.背景 最近项目中Android设备需要获取SoftAP信息(wifi账号.密码.IP等),然后传递到投屏器中,那么如何获取到SoftAP信息呢?我们知道可以通过WifiManager类里的方法可以 ...

  4. 建民的JAVA课堂

    import javax.swing.JOptionPane; public class Main { public static void main(String[] args) { String ...

  5. python面向对象编程(封装、隐藏)

    一 封装 1.封装介绍封装是面向对象三大特性最核心的一个特性封装<----->整合2.将封装的属性进行隐藏操作1).如何隐藏:在属性名前加__前缀,就会实现一个对外隐藏属性效果该隐藏需要注 ...

  6. DialogFragment源码分析

    目录介绍 1.最简单的使用方法 1.1 官方建议 1.2 最简单的使用方法 1.3 DialogFragment做屏幕适配 2.源码分析 2.1 DialogFragment继承Fragment 2. ...

  7. proteus的五状态显示控制器

    proteus的五状态显示控制器 1.实验原理 使用的核心器件还是4028,BCD译码器.将输入的四个信号接入输入端,输出信号选取0.1.2.4.8这五个输出状态驱动led显示.发光LED需要加入保护 ...

  8. redis的三种模式

    一.主从模式 1.主从模式是最为简单的redis集群模式 2.主要工作模式是主从复制.主数据库可以执行读写功能,而从数据库只能执行读功能.主数据库数据发生变化,会自动同步到从数据库. 3.主数据库为m ...

  9. 初识urllib与requests

    urllib与requests 一.urllib的学习 学习目标 了解urllib的基本使用 1.urllib介绍 除了requests模块可以发送请求之外, urllib模块也可以实现请求的发送,只 ...

  10. #线段树,矩阵乘法#LOJ 3264「ROIR 2020 Day 2」海报

    题目 分析 设\(dp[i][0/1/2/3]\)表示以\(i\)结尾1的长度为0/1/2/3的最大值, 那么 \[\begin{cases}dp[i][0]=\max\{dp[i-1][\dots] ...