0

I'm trying to write a python client to listen to a gRPC stream (fire hose). It constantly keeps streaming. There is no "on completion".

Proto:

rpc Start (StartParameters) returns (stream Progress) {}

In the client I tried writing the following, but as the Start rpc does not return "on complete", I don't get the control to the for loop to print (event).

rsp = self.stub.Start(params)
for event in rsp:
    print(event)

Can somebody please help me with a python codeto handle or capture all the events in rsp after a timeout (2 mins) and then print each event in rsp.

Vlad
  • 8,225
  • 5
  • 33
  • 45
Ram
  • 13
  • 3

1 Answers1

1

I got this working, posting this incase if somebody else is looking for an answer

   def collect_responses(self, response_iterator, response_queue):
      for response in response_iterator:
         response_queue.put(response)


   def call_rpc(self)
      response_stream = stub.Start(params)
      response_queue = queue.Queue()
      thread = threading.Thread(target=self.collect_responses,
                                args=(response_stream, response_queue))
      thread.start()

      time.sleep(120) # or have a different trigger to say, cancel stream

      response_stream.cancel()
      thread.join()

      while not response_queue.empty():
         item = response_queue.get()
         print(item)
Ram
  • 13
  • 3