Please could someone help me with the Google Pubsub Python Client Library? I am following the tutorial at https://cloud.google.com/pubsub/docs/pull#pubsub-pull-messages-async-python closely and seem to get unprompted errors. I have a simple script called "sendmessage.py" that sends a text message with a random number appended so that I can tell messages apart. The subscriber code runs on a separate compute engine instance and looks like this:
from google.cloud import pubsub_v1
def callback(message):
print('Received message: {}'.format(message))
message.ack()
def listen_for_errors():
subscriber = pubsub_v1.SubscriberClient()
subscription_path = subscriber.subscription_path('<my-project-name-here>', 'test-subscription')
subscription = subscriber.subscribe(subscription_path, callback=callback)
try:
subscription.future.result()
except Exception as e:
print(
'Listening for messages on {} threw an Exception: {}.'.format( 'test-subscription', e))
raise
A screenshot of the send/receive running on two compute instances is attached. The system seems to work fine for the first minute or so, then the subscriber seems to trip up with the following error message:
Exception in thread Thread-ConsumeBidirectionalStream:
Traceback (most recent call last):
File "/usr/lib/python2.7/threading.py", line 801, in __bootstrap_inner
self.run()
File "/usr/lib/python2.7/threading.py", line 754, in run
self.__target(*self.__args, **self.__kwargs)
File "/usr/local/lib/python2.7/dist-packages/google/cloud/pubsub_v1/subscribe
r/_consumer.py", line 363, in _blocking_consume
request_generator, response_generator)
File "/usr/local/lib/python2.7/dist-packages/google/cloud/pubsub_v1/subscribe
r/_consumer.py", line 275, in _stop_request_generator
if not response_generator.done():
AttributeError: '_StreamingResponseIterator' object has no attribute 'done'
This happens after a short time (less than a few minutes) even if no messages are sent. Once it has crashed, there is no way to recover - e.g. by pressing enter, typing quit(), pressing CTRL+C, etc, so I have to shutdown the instance and start over.
I find it a bit strange that I am following the tutorials so closely and yet there are unprompted errors when my code is running. Please would it be possible for someone to point out where I have gone wrong or suggest a robust workaround to ignore the error and keep listening for messages?
Kind regards and thank you to anyone who can help,
Paul