Google Pubsub Python 客户端库订阅者随机崩溃

2024-04-06

请问有人可以帮助我使用 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. Pubsub error 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.

我觉得有点奇怪,我如此严格地遵循教程,但在我的代码运行时却出现了无提示的错误。请问有人可以指出我出错的地方或建议一个可靠的解决方法来忽略错误并继续监听消息吗?


发布问题后不久,我尝试在自己的计算机上运行相同的脚本,发现它们非常稳定,这使我得出结论,问题出在计算引擎实例的设置方式上。

我原来的启动代码包括以下几行:

sudo apt-get update
sudo apt-get -yq install python-pip
sudo apt-get install python-dev
sudo pip install --upgrade google-cloud-storage
sudo pip install --upgrade google-cloud-pubsub

我想知道最后一行是否应该引用 pubsub_v1,但无论如何我将引导脚本代码更改为:

sudo apt-get update
sudo apt-get -yq install python-pip
sudo pip install --upgrade google-cloud

问题似乎已经解决了。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Google Pubsub Python 客户端库订阅者随机崩溃 的相关文章

随机推荐