python - Google Cloud PubSub 在异步拉取消息时抛出 504 Deadline Exceeded 错误
问题描述
我有一个订阅 PubSub 主题并使用异步拉取的服务。在闲置 10 分钟且未收到任何消息后,PubSub 会引发 504 Deadline exceeded 错误。
错误总是在大约 10 分钟后发生。我发现的每个类似问题都与同步拉动有关,而不是我使用的异步拉动。
错误信息:
INFO:google.cloud.pubsub_v1.subscriber._protocol.streaming_pull_manager:Observed non-terminating stream error 504 Deadline Exceeded
INFO:google.cloud.pubsub_v1.subscriber._protocol.streaming_pull_manager:Observed recoverable stream error 504 Deadline Exceeded
INFO:google.api_core.bidi:Re-established stream
INFO:google.cloud.pubsub_v1.subscriber._protocol.streaming_pull_manager:Observed non-terminating stream error 504 Deadline Exceeded
INFO:google.cloud.pubsub_v1.subscriber._protocol.streaming_pull_manager:Observed recoverable stream error 504 Deadline Exceeded
订阅者类别:
from google.cloud import pubsub_v1
class Subscriber():
def __init__(self):
self.subscriber = pubsub_v1.SubscriberClient()
self.project_id = "my-project-id"
self.subscription_id = "SUBSCRIPTION_MAIL_RECIEVE"
self.subscription_path = self.subscriber.subscription_path(self.project_id,
self.subscription_id)
def subscribe(self, callback):
"""
Parameters:
callback (callable): function to be called for incoming messages on given topic
"""
streaming_pull_future = self.subscriber.subscribe(self.subscription_path,
callback=callback)
return streaming_pull_future
监听消息:
subscriber = Subscriber()
pull_future = subscriber.subscribe(my_callback_function(message))
with subscriber.subscriber:
try:
print("Waiting for messages")
pull_future.result()
except TimeoutError:
pull_future.cancel()
解决方案
这是正常行为,当您的流重新建立时,可以忽略这些消息。如果没有重新建立流并且没有发送心跳,那么您可能需要将您的谷歌核心 API 和谷歌云 Pub/Sub 升级到最新版本。
pip install google-cloud-pubsub --upgrade
pip install google-api-core --upgrade
您还可以按照链接https://github.com/googleapis/google-cloud-python/issues/5800中的建议过滤这些错误消息
推荐阅读
- stackdriver - Which is the api call for retrieving the events of a policy in Google Stackdriver?
- java - Eclipse plugin - issue passing empty command arguments from option preference in plugin.xml
- java - Update of an entity with ttl not working as expected with Datastax Java driver
- node.js - OVH 对象存储,当我尝试上传大文件(超过 100 Ko)时没有任何反应
- python - 回溯数独仅适用于 Python 3
- javascript - Request failed for https://docs.google.com returned code 400. Truncated server response:
- android - Android Studio 在我的发布 apk 名称中附加了“脏”字是什么意思?
- python - ValueError: setting an array element with a sequence directly causes snother error TypeError: only size-1 arrays can be converted to Python scalars
- swift - Firebase 需要很长时间来编写文档并冻结应用程序
- r - 如果我的月、日和年在不同的列中,我可以使用 R 包 lubridate 来解析日期吗?