4
我正在嘗試建立一個長時間運行的Pull訂閱Google雲PubSub主題。 我使用非常相似的文檔here,即在給出的示例代碼:Google PubSub python客戶端返回StatusCode.UNAVAILABLE
def receive_messages(project, subscription_name):
"""Receives messages from a pull subscription."""
subscriber = pubsub_v1.SubscriberClient()
subscription_path = subscriber.subscription_path(
project, subscription_name)
def callback(message):
print('Received message: {}'.format(message))
message.ack()
subscriber.subscribe(subscription_path, callback=callback)
# The subscriber is non-blocking, so we must keep the main thread from
# exiting to allow it to process messages in the background.
print('Listening for messages on {}'.format(subscription_path))
while True:
time.sleep(60)
的問題是,我收到有時以下回溯:
Exception in thread Consumer helper: consume bidirectional stream:
Traceback (most recent call last):
File "/usr/lib/python3.5/threading.py", line 914, in _bootstrap_inner
self.run()
File "/usr/lib/python3.5/threading.py", line 862, in run
self._target(*self._args, **self._kwargs)
File "/path/to/google/cloud/pubsub_v1/subscriber/_consumer.py", line 248, in _blocking_consume
self._policy.on_exception(exc)
File "/path/to/google/cloud/pubsub_v1/subscriber/policy/thread.py", line 135, in on_exception
raise exception
File "/path/to/google/cloud/pubsub_v1/subscriber/_consumer.py", line 234, in _blocking_consume
for response in response_generator:
File "/path/to/grpc/_channel.py", line 348, in __next__
return self._next()
File "/path/to/grpc/_channel.py", line 342, in _next
raise self
grpc._channel._Rendezvous: <_Rendezvous of RPC that terminated with (StatusCode.UNAVAILABLE, The service was unable to fulfill your request. Please try again. [code=8a75])>
我看到這個在another question中被引用,但在這裏我要求如何在Python中正確處理它。我試圖在異常中包裝請求,但它似乎在後臺運行,並且在出現該錯誤時我無法重試。
FWIW,大約一個小時後出現[cpu問題](https://github.com/GoogleCloudPlatform/google-cloud-python/issues/3965)。我認爲這個問題在每次忽略錯誤時都是線程泄漏(其他人在DEADLINE_EXCEEDED之後得到它),但是我找不到修復程序。我回到使用舊的api,並做我自己的定期拉,而不是使用流的東西。 [這個例子](https://github.com/GoogleCloudPlatform/cloud-pubsub-samples-python/blob/master/cmdline-pull/pubsub_sample.py)對設置起來很有用。 –
感謝您的回覆......對於那些應該非常簡單的東西來說,這看起來太過於誇張了。對於手頭的任務,我最終使用了'golang'客戶端庫,它的功能如同魅力 – adrpino
[github上的相關問題](https://github.com/GoogleCloudPlatform/google-cloud-python/issues/2683) – Blackus