Repeated field is empty on the client side even though it is set on the server side

579 views
Skip to first unread message

jiongj...@gmail.com

unread,
Sep 10, 2018, 6:05:24 AM9/10/18
to grpc.io
The repeated field of protobuf message is always empty on the gRPC client side even though I set it on the gRPC server side.
Is there any way to fix this issue?


  • helloworld.proto file:
    syntax = "proto3";

    package helloworld;

    // The greeting service definition.
   service Greeter {
     // Sends a greeting.
     rpc SayHello (HelloRequest) returns (HelloReply) {}
   }

    // The request message containing the user's name.
   message HelloRequest {
     string name = 1;
   }

    // The response message containing the greetings.
   message HelloReply {
     string message = 1;
     repeated string snippets = 2;
   }




  • server.py
    from __future__ import absolute_import
   from __future__ import division
   from __future__ import print_function

    from concurrent import futures
   import time

    import grpc

    import grpc_demo.lib.protos.helloworld_pb2 as helloworld_pb2
   import grpc_demo.lib.protos.helloworld_pb2_grpc as helloworld_pb2_grpc


    _ONE_DAY_IN_SECONDS = 60 * 60 * 24

    class Greeter(helloworld_pb2_grpc.GreeterServicer):

        def SayHello(self, request, context):
           reply = helloworld_pb2.HelloReply(message='Hello, %s!' % request.name)
           reply.snippets.append('test-snippets')
           # print('reply:' + reply)
           return reply

    def main():
       """Main entrance."""

        server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
       helloworld_pb2_grpc.add_GreeterServicer_to_server(Greeter(), server)
       server.add_insecure_port('[::]:50051')
       server.start()
       try:
           while True:
               time.sleep(_ONE_DAY_IN_SECONDS)
       except KeyboardInterrupt:
           server.stop(0)


    if __name__ == '__main__':
       main()




  • client.py
    from __future__ import absolute_import
   from __future__ import division
   from __future__ import print_function

    import grpc

    import grpc_demo.lib.protos.helloworld_pb2 as helloworld_pb2
   import grpc_demo.lib.protos.helloworld_pb2_grpc as helloworld_pb2_grpc

    def main():
       """Main entrance."""

        # NOTE(gRPC Python Team): .close() is possible on a channel and should be
       # used in circumstances in which the with statement does not fit the needs
       # of the code.
       with grpc.insecure_channel('localhost:50051') as channel:
           stub = helloworld_pb2_grpc.GreeterStub(channel)
           response = stub.SayHello(helloworld_pb2.HelloRequest(name='you'))
       print("Greeter client received: " + response.message + ', '.join(response.snippets)
            + str(len(response.snippets)))


    if __name__ == '__main__':
       main()



  • Expected Result:

Greeter client received: Hello, you!test-snippets1

  • Actual Result:

Greeter client received: Hello, you!0

Thanks!
Jiongjiong Li

Carl Mastrangelo

unread,
Sep 10, 2018, 1:35:29 PM9/10/18
to grpc.io
As a sanity check, are the client and server both using the same generated code?  Protobuf will ignore fields it doesn't know about by default.  In your case, it sounds like you may have modified the proto on one side.  Is this the case?

jiongj...@gmail.com

unread,
Sep 10, 2018, 9:45:32 PM9/10/18
to grpc.io
No, both the client and server run in the same anaconda environment and the proto import path are the same.

Thanks & Best Wishes!
Jiongjiong Li

jiongj...@gmail.com

unread,
Sep 10, 2018, 10:17:47 PM9/10/18
to grpc.io
Finally it worked after running command below, though the reason for this issue is not clear: 

killall python


Thanks & Best Wishes!
Jiongjiong Li


On Tuesday, September 11, 2018 at 1:35:29 AM UTC+8, Carl Mastrangelo wrote:
Reply all
Reply to author
Forward
0 new messages