如何让 gRPC 正确序列化响应

How to get gRPC to properly serialize response

在尝试简单的 gRPC 实现时出现奇怪的错误,即遵循标准 python 示例。服务器似乎 运行 OK,但是当我用客户端 ping 时出现错误

grpc:

package pas;

// The PAS service definition
service PAS {
  // analyze single file
  rpc getPhotonRecords (PhotonRecordsRequest) returns (PhotonRecordsReply) {}
}

message PhotonRecordsRequest {
  string fileName = 1;
}

message PhotonRecordsReply {
  repeated uint32 PhotonRecords = 1;
}

客户:

with grpc.insecure_channel("localhost:50051") as channel:
        stub = pas_pb2_grpc.PASStub(channel)
        msg = pas_pb2.PhotonRecordsRequest(fileName='testingFilename.flb')
        response = stub.getPhotonRecords(msg)

服务器:

class PAS_GRPC(pas_pb2_grpc.PASServicer):

    def getPhotonRecords(self, request: pas_pb2.PhotonRecordsRequest, context):
        # check for required fields and error if not there or valid
        # update any optional fields that the request has specified
        PhotonRecordsReply = pas_pb2.PhotonRecordsReply()
        PhotonRecordsReply.PhotonRecords.extend([1, 3, 7])
        return pas_pb2.PhotonRecordsReply

客户端错误:

<_InactiveRpcError of RPC that terminated with:
    status = StatusCode.INTERNAL
    details = "Failed to serialize response!"

服务器错误:

TypeError: IsInitialized() missing 1 required positional argument: 'self'

您的服务器方法 getPhotonRecords return类型:

return pas_pb2.PhotonRecordsReply

但它应该return您创建的变量:

return PhotonRecordsReply

您可能希望对变量使用 snake_case 以帮助区分 CamelCase class 名称,即:

photon_records_reply = pas_pb2.PhotonRecordsReply()
...