Presently if you subscribe and an error occurs in the consumer's thread there's no way to respond to the error on the main thread.
It just logs the exception and hangs:
ExceptioninthreadConsumerhelper: consumebidirectionalstream:
Traceback (mostrecentcalllast):
File"/Users/jonwayne/.pyenv/versions/3.6.0/lib/python3.6/threading.py", line916, in_bootstrap_innerself.run()
File"/Users/jonwayne/.pyenv/versions/3.6.0/lib/python3.6/threading.py", line864, inrunself._target(*self._args, **self._kwargs)
File"/Users/jonwayne/workspace/python-docs-samples/pubsub/cloud-client/env/lib/python3.6/site-packages/google/cloud/pubsub_v1/subscriber/_consumer.py", line248, in_blocking_consumeself._policy.on_exception(exc)
File"/Users/jonwayne/workspace/python-docs-samples/pubsub/cloud-client/env/lib/python3.6/site-packages/google/cloud/pubsub_v1/subscriber/policy/thread.py", line135, inon_exceptionraiseexceptionFile"/Users/jonwayne/workspace/python-docs-samples/pubsub/cloud-client/env/lib/python3.6/site-packages/google/cloud/pubsub_v1/subscriber/_consumer.py", line234, in_blocking_consumeforresponseinresponse_generator:
File"/Users/jonwayne/workspace/python-docs-samples/pubsub/cloud-client/env/lib/python3.6/site-packages/grpc/_channel.py", line363, in__next__returnself._next()
File"/Users/jonwayne/workspace/python-docs-samples/pubsub/cloud-client/env/lib/python3.6/site-packages/grpc/_channel.py", line357, in_nextraiseselfgrpc._channel._Rendezvous: <_RendezvousofRPCthatterminatedwith (StatusCode.NOT_FOUND, Resourcenotfound (resource=my-subs).)>
It seems that consumer.open should return a future. This future could be used to both block the main thread to consume message and pass through exceptions.
So the current sample:
defreceive_messages(project, subscription_name):
"""Receives messages from a pull subscription."""subscriber=pubsub_v1.SubscriberClient()
subscription_path=subscriber.subscription_path(
project, subscription_name)
defcallback(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))
whileTrue:
time.sleep(60)Could instead be:
defreceive_messages(project, subscription_name):
"""Receives messages from a pull subscription."""subscriber=pubsub_v1.SubscriberClient()
subscription_path=subscriber.subscription_path(
project, subscription_name)
defcallback(message):
print('Received message: {}'.format(message))
message.ack()
future=subscriber.subscribe(subscription_path, callback=callback)
print('Listening for messages on {}'.format(subscription_path))
# The subscriber is non-blocking but returns a future that allows us# to block the main thread and allow messages to process in the# background. This effectively blocks forever unless an error# occurs in the subscriber thread in which case it'll be# re-raised here.future.result()
Presently if you subscribe and an error occurs in the consumer's thread there's no way to respond to the error on the main thread.
It just logs the exception and hangs:
It seems that
consumer.openshould return a future. This future could be used to both block the main thread to consume message and pass through exceptions.So the current sample:
Could instead be: