Leopoldo
Leopoldo

Reputation: 845

how to use grpc stream

I am trying to use hellostreamingworld.proto from grpc tutorial, to change helloworld example.

I have created multi_greeter_server.py

from concurrent import futures
import time

import grpc

import hellostreamingworld_pb2
import hellostreamingworld_pb2_grpc

_ONE_DAY_IN_SECONDS = 60 * 60 * 24


class MultiGreeter(hellostreamingworld_pb2_grpc.MultiGreeterServicer):
  def sayHello(self, request, context):
    print("Received message: {}".format(request))
    for greet in range(int(request.num_greetings)):
        yield hellostreamingworld_pb2.HelloReply(message='Hello, %s!' % request.name)


def serve():
  server = grpc.server(futures.ThreadPoolExecutor(max_workers=10))
  hellostreamingworld_pb2_grpc.add_MultiGreeterServicer_to_server(MultiGreeter(), 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__':
  serve()

And I created also multigreeter_client.py

from __future__ import print_function

import grpc

import hellostreamingworld_pb2
import hellostreamingworld_pb2_grpc


def run():
  channel = grpc.insecure_channel('localhost:50051')
  stub = hellostreamingworld_pb2_grpc.MultiGreeterStub(channel)
  response = stub.sayHello(hellostreamingworld_pb2.HelloRequest(name='Marian', num_greetings='5'))
  print("Greeter client received: " + response.message)

Unfortunately I cannot see communication between Client and Server. When I try to run client (when server is running) There is a Traceback:

Traceback (most recent call last):
  File "multi_greeter_client.py", line 35, in <module>
    run()
  File "multi_greeter_client.py", line 29, in run
    print("Greeter client received: " + response.message)
AttributeError: '_Rendezvous' object has no attribute 'message'

Upvotes: 1

Views: 2964

Answers (1)

Leopoldo
Leopoldo

Reputation: 845

Now I get it. Response is returned as an iterator. So it should be handle as iterator.

Upvotes: 3

Related Questions