请问有人可以帮助我使用 Google Pubsub Python 客户端库吗?我正在关注以下教程https://cloud.google.com/pubsub/docs/pull#pubsub-pull-messages-async-python https://cloud.google.com/pubsub/docs/pull#pubsub-pull-messages-async-python密切关注,似乎会出现无提示的错误。我有一个名为“sendmessage.py”的简单脚本,它发送一条附加随机数的文本消息,以便我可以区分消息。订阅者代码在单独的计算引擎实例上运行,如下所示:
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.
我觉得有点奇怪,我如此严格地遵循教程,但在我的代码运行时却出现了无提示的错误。请问有人可以指出我出错的地方或建议一个可靠的解决方法来忽略错误并继续监听消息吗?