How to pass array in a gRPC?

0

I am testing some codes using gRPC with the Python language, and I am not able to manipulate array. The code consists of passing one vector per parameter, the server receives that vector and sorts.

  

.proto code

syntax = "proto3";

message Number {
  repeated int32 value = 1;

}
  

Code orders.py

def organiza(vetor):
vetor.sort()
return vetor
  

server.py

class OrdenaServicer(ordena_pb2_grpc.OrdenaServicer):

def Organiza(self, request, context):
    response = ordena_pb2.Number()
    response.value = int(ordena.organiza(request.value))
    return response


service Ordena {
  rpc Organiza(Number) returns (Number) {}
}
  

client.py

# create a stub (client)
stub = ordena_pb2_grpc.OrdenaStub(channel)

# create a valid request message
vetor = [23,65,8,3,89,34,6,22,12,5,9,54]
number = ordena_pb2.Number(value=vetor)

# make the call
response = stub.Organiza(number)

The code itself is very simple, it is only a basic sort, but I can not pass the vector as a parameter. Whenever I try to get an error:

  

TypeError: int () argument must be a string, a byte-like object or a number, not 'google.protobuf.pyext._message.RepeatedScalarContainer'

    
asked by anonymous 15.06.2018 / 23:01

1 answer

2

The Organiza service expects an instance of the Number class to return. To instantiate an object of this class, you need to pass a list of integers as an argument, since this is what was specified in the proto. Therefore, below is the correct service code:

def Organiza(self, request, context):
    vetor_ordenado = ordena.organiza(request.value)
    return ordena_pb2.Number(value=vetor_ordenado)

But the main reason the TypeError exception arose was because you tried to convert a list to an integer:

int(ordena.organiza(request.value))
    
16.06.2018 / 06:55