首页 > 解决方案 > 为什么我的 gRPC 响应内容在服务器和客户端上有所不同?

问题描述

我正在尝试使用 gRPC 对服务器进行客户端调用。服务器准备了一个响应,其中包含我期望的内容,但是当我检查客户端上的响应对象时,它缺少数据。

以下是我的原型文件的相关部分:

message DetectionBoxes {
    float min_x = 1;
    float min_y = 2;
    float max_x = 3;
    float max_y = 4;
}
message SignatureResponseObject {
    DetectionBoxes detection_box = 1;
    DetectionClass detection_class = 2;
    float detection_score = 3;
}
message SignatureResponse {
    repeated SignatureResponseObject signatures = 1;
}

service SignatureService {
    rpc FindSignatures(SignatureRequest) returns (SignatureResponse);
}

然后是服务器代码的相关部分:

if r.detection_class == "signature":
                        signature_response_object = example_pb2.SignatureResponseObject()

                        signature_response_object.detection_class = example_pb2.DetectionClass.SIGNATURE
                        detection_box = example_pb2.DetectionBoxes(
                            min_x=r.min_x,
                            min_y=r.min_y,
                            max_x=r.max_x,
                            max_y=r.max_y
                        )
                        signature_response_object.detection_box.CopyFrom(detection_box)
                        # signature_response_object.detection_box.min_x = r.min_x
                        # signature_response_object.detection_box.min_y = r.min_y
                        # signature_response_object.detection_box.max_x = r.max_x
                        # signature_response_object.detection_box.max_y = r.max_y
                        signature_response_object.detection_score = r.score
                        signature_response.signatures.append(signature_response_object)

...

print(signature_response)

如您所见,我尝试使用 CopyFrom 并直接分配值。然后打印出来的是:

signatures {
  detection_box {
    min_x: 0.3274773359298706
    min_y: 0.7998588681221008
    max_x: 0.573384702205658
    max_y: 0.8861611485481262
  }
  detection_class: SIGNATURE
  detection_score: 0.999962568283081
}

正如预期的那样。但是在客户端上,我得到了这个:

client = example_pb2_grpc.SignatureServiceStub(channel)

    request = example_pb2.SignatureRequest(
        image=byte_image   
    )

    response = client.FindSignatures(request=request)

    print(response)

有了这个打印声明给我:

signatures {
  detection_box {
  }
  detection_class: SIGNATURE
  detection_score: 0.99996256828

我很困惑,因为我不确定为什么detection_box会在服务器上分配,但在客户端上什么都没有。为了澄清起见,我尝试打印出的特定字段,detection_box它们都是0.

任何帮助是极大的赞赏!

编辑:

为了进行完整性检查,我通过网络截获了 gRPC 流量,并且消息与预期一致。

{"message_origin":"server","raw_message":"Ch0KFA0cq6c+FY3DTD8dV8kSPyV122I/EAIdjP1/Pw==","message":{"1":{"1":{"1":0.32747734,"2":0.79985887,"3":0.5733847,"4":0.88616115},"2":"2","3":0.99996257}},"timestamp":"2021-05-14T09:26:58.179817682-05:00"}],"metadata":{":authority":["localhost:35961"],"accept-encoding":["identity,gzip"],"content-type":["application/grpc"],"grpc-accept-encoding":["identity,deflate,gzip"],"user-agent":["grpc-python/1.32.0 grpc-c/12.0.0 (linux; chttp2)"],"via":["HTTP/2.0 127.0.0.1:35961"]},"metadata_response_headers":{"accept-encoding":["identity,gzip"],"content-type":["application/grpc"],"grpc-accept-encoding":["identity,deflate,gzip"]},"metadata_response_trailers":{}}

EDIT2:我还删除了所有pycache目录并再次尝试。没有帮助。

标签: pythonprotocol-buffersgrpcprotogrpc-python

解决方案


推荐阅读