grpc stream剖析

在grpc里可以指定一个服务器端的流方法。比如流视频传输。不是返回一个响应,而是返回零个或多个响应
使用官方python版route guide例子,server端流模式,client端普通模式,
经过如下小的修改,对实际的一些行为进行一些探究
服务端
    i = 0   #新增
    def ListFeatures(self, request, context):
        global i #新增
        i = i + 1  #新增
        print(i)  #新增

        left = min(request.lo.longitude, request.hi.longitude)
        right = max(request.lo.longitude, request.hi.longitude)
        top = max(request.lo.latitude, request.hi.latitude)
        bottom = min(request.lo.latitude, request.hi.latitude)

        y=0  #新增
        for feature in self.db:
            y = y+1  #新增
            print(y)  #新增
            if (feature.location.longitude >= left and
                    feature.location.longitude <= right and
                    feature.location.latitude >= bottom and
                    feature.location.latitude <= top):
                yield feature

 客户端

def guide_list_features(stub):
    rectangle = route_guide_pb2.Rectangle(
        lo=route_guide_pb2.Point(latitude=400000000, longitude=-750000000),
        hi=route_guide_pb2.Point(latitude=420000000, longitude=-730000000))
    print("Looking for features between 40, -75 and 42, -73")

    features = stub.ListFeatures(rectangle)
    i=0  #新增

    for feature in features:
        i = i+1  #新增
        if i > 10:  #新增
          break  #新增

        print("get No.:%d" % i)
        print("Feature called %s at %s" % (feature.name, feature.location))

  

1、client发起一次请求,server会返回多个响应,此时server方法执行几次
1次
2、若client只接收了一部分响应,server还会生成所有的响应吗
不会,比如客户端解析了10个响应,服务端本应生成100个响应,但服务端可能只会产生20或30条响应,不会把所有的结果集全处理到响应里。就像流一样,下游的行为会影响上游情况
 
原文地址:https://www.cnblogs.com/codetouse/p/14363335.html