Google Pubsub Python客户端库订阅者随机崩溃 [英] Google Pubsub Python Client library subscriber crashes randomly

查看:60
本文介绍了Google Pubsub Python客户端库订阅者随机崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请有人可以帮助我使用Google Pubsub Python客户端库吗?我在 https://cloud上关注该教程. google.com/pubsub/docs/pull#pubsub-pull-messages-async-python 密切相关,并且似乎会出现无提示的错误.我有一个名为"sendmessage.py"的简单脚本,该脚本发送带有附加随机数的文本消息,以便我可以区分消息.订户代码在单独的计算引擎实例上运行,如下所示:

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'

这会在短时间内(少于几分钟)发生,即使没有消息发送.一旦崩溃,便无法恢复-例如通过按Enter键,键入quit(),按 CTRL + C 等,因此我必须关闭实例并重新开始.

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,

保罗

推荐答案

我开始养成对自己的问题进行回答的习惯(这可能意味着在发布到此处之前,我应该进行更多的研究或进行更多的尝试!),但我相信我已经解决了上述问题.我以为我可以在这里发布答案,以防其他人好奇或遇到相同的问题,但是如果主持人希望删除该主题,请这样做.

Im starting to make a habit of responding to my own questions (which possibly means that I should have researched more or tried a little harder before posting here!), but I believe I have resolved the issue above. I thought I would post the answer here in case others are curious or run into the same problem, but if a moderator would prefer to delete the topic then please do so.

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

Shortly after posting my problem, I tried running the same scripts on my own computer and discovered that they were highly stable, which led me to conclude that the problem was with the way that the compute engine instances were set up.

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

My original boot code included the lines:

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,但是无论如何我都将引导脚本代码更改为:

I wonder whether the last line should have referenced pubsub_v1, but in any case I changed the bootscript code to this:

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

似乎已经解决了问题.

这篇关于Google Pubsub Python客户端库订阅者随机崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆