Search code examples
grpcgrpc-python

How to get gRPC to properly serialize response


getting a strange error when trying simple gRPC implementation I.e. following the standard python example. Server seems to run OK, but get the error when I ping it with a client

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;
}

client:

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)

server:

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

client error:

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

server error:

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

Solution

  • Your server method getPhotonRecords returns the type:

    return pas_pb2.PhotonRecordsReply
    

    But it should return the variable you created:

    return PhotonRecordsReply
    

    You may want to use snake_case for variables to help differentiate from CamelCase class names, i.e.:

    photon_records_reply = pas_pb2.PhotonRecordsReply()
    ...