(三)grpc-流式传输

时间:2021-06-06 21:22:24   收藏:0   阅读:30

1.单向流:服务端向客户端流传输

创建一个protobuf 文件:

hello_world.proto

// 定义一个服务的框架,服务名和服务下的函数名,以及函数下的request 和response,
// 和resquest 和response 对应的参数

//使用哪种protobuf 协议
syntax="proto3";
package test;

//服务名:Mianmian,以及里面定义的rpc函数HelloMian
service Mianmian{
    rpc HelloMian(HelloMianReq) returns(HelloMianReply)
    {}   //添加一些插件
    rpc TestClientRecvStream(TestClientRecvStreamRequest) returns(stream TestClientRecvStreamRequest){}
}

//传输方式
// 1.unary 单程
// 2.stream  1. 双向 客户端请求服务端(流),服务端返回客户端(流)
//             2.单向 服务端接收客户端(流),服务端返回客户端(非流)
//            3. 单向 服务端接收客户端(非流),服务器send客户端(流)
message HelloMianReq{
    string  name = 1;
    int32 age =2;
}

message HelloTestRuest{
    string  name = 1;
    int32 age =2;
    repeated string data = 3; //a=[1,2]
    map<string,int32> number=4; //string ,int32
}

message TestClientRecvStreamRequest{
    string data=1;
}
message  TestClientRecvStreamResponse{
    string result=1;
}
message HelloMianReply{
    string result =1;
}


message HelloTestResponse{
    string result =1;
}

编写service 代码:

service.py

#!/usr/bin/env python
# -*- coding:utf-8 -*-
#@Time  : 2021/6/5 13:37
#@Author: Tana
#@File  : service.py

import grpc
import hello_world_pb2 as pb2
import hello_world_pb2_grpc as pb2_grpc
from concurrent import futures
import time
class Mianmian(pb2_grpc.MianmianServicer):
    def HelloMian(self, request, context):
        name=request.name
        age=request.age

        result={"code":"succeed",
            "data":fmy name is {name},i am {age} years old
        }
        return pb2.HelloMianReply(result=result)
    def TestClientRecvStream(self, request, context):
        #判断客户端是否活跃
        index=0
        while context.is_active():
            data=request.data
            time.sleep(1)
            index += 1
            yield pb2.TestClientRecvStreamResponse(
                result=fsend {index} {data}
            )


def run():
    #定义grpc线程数量
    grpc_server=grpc.server(futures.ThreadPoolExecutor(max_workers=4))
    #注册服务到grpc_server
    pb2_grpc.add_MianmianServicer_to_server(Mianmian(),grpc_server)
    #绑定ip和端口号
    grpc_server.add_insecure_port(0.0.0.0:5001)
    print("server will start at 0.0.0.0:5001")
    #这个start 在python里面会启动一下就停了,所以需要写一个无限循环
    grpc_server.start()
    try:
        while 1:
            print("1")
            time.sleep(3600)

    except KeyboardInterrupt:
        grpc_server.stop(0)

if __name__=="__main__":
    run()

编写client端 代码:

client.py

#!/usr/bin/env python
# -*- coding:utf-8 -*-
#@Time  : 2021/6/6 15:26
#@Author: Tana
#@File  : client.py
import grpc
import hello_world_pb2_grpc as pb2_grpc
import hello_world_pb2 as pb2

def run():
    #定义一个频道,绑定服务器端对应的ip 和端口号
    conn = grpc.insecure_channel(0.0.0.0:5000)
    #生成客户端
    client = pb2_grpc.MianmianStub(channel=conn)
    # response = client.HelloMian(pb2.HelloMianReq(
    #     name="mianmian",
    #     age=18
    # ))
    # print(response.result)
    #
    response=client.TestClientRecvStream(pb2.TestClientRecvStreamRequest(
        data=mianmian
    ))

    for item in response:
        print(item.result)


if  __name__==__main__:
    run()

 

原文:https://www.cnblogs.com/yan-2010/p/14855763.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!