def subscribe( self, subscription, callback, flow_control=(), scheduler=None, use_legacy_flow_control=False, ): """Asynchronously start receiving messages on a given subscription. This method starts a background thread to begin pulling messages from a Pub/Sub subscription and scheduling them to be processed using the provided ``callback``. The ``callback`` will be called with an individual :class:`google.cloud.pubsub_v1.subscriber.message.Message`. It is the responsibility of the callback to either call ``ack()`` or ``nack()`` on the message when it finished processing. If an exception occurs in the callback during processing, the exception is logged and the message is ``nack()`` ed. The ``flow_control`` argument can be used to control the rate of at which messages are pulled. The settings are relatively conservative by default to prevent "message hoarding" - a situation where the client pulls a large number of messages but can not process them fast enough leading it to "starve" other clients of messages. Increasing these settings may lead to faster throughput for messages that do not take a long time to process. The ``use_legacy_flow_control`` argument disables enforcing flow control settings at the Cloud PubSub server and uses the less accurate method of only enforcing flow control at the client side. This method starts the receiver in the background and returns a *Future* representing its execution. Waiting on the future (calling ``result()``) will block forever or until a non-recoverable error is encountered (such as loss of network connectivity). Cancelling the future will signal the process to shutdown gracefully and exit. .. note:: This uses Pub/Sub's *streaming pull* feature. This feature properties that may be surprising. Please take a look at https://cloud.google.com/pubsub/docs/pull#streamingpull for more details on how streaming pull behaves compared to the synchronous pull method. Example: .. code-block:: python from google.cloud import pubsub_v1 subscriber_client = pubsub_v1.SubscriberClient() # existing subscription subscription = subscriber_client.subscription_path( 'my-project-id', 'my-subscription') def callback(message): print(message) message.ack() future = subscriber_client.subscribe( subscription, callback) try: future.result() except KeyboardInterrupt: future.cancel() Args: subscription (str): The name of the subscription. The subscription should have already been created (for example, by using :meth:`create_subscription`). callback (Callable[~google.cloud.pubsub_v1.subscriber.message.Message]): The callback function. This function receives the message as its only argument and will be called from a different thread/ process depending on the scheduling strategy. flow_control (~google.cloud.pubsub_v1.types.FlowControl): The flow control settings. Use this to prevent situations where you are inundated with too many messages at once. scheduler (~google.cloud.pubsub_v1.subscriber.scheduler.Scheduler): An optional *scheduler* to use when executing the callback. This controls how callbacks are executed concurrently. This object must not be shared across multiple SubscriberClients. Returns: A :class:`~google.cloud.pubsub_v1.subscriber.futures.StreamingPullFuture` instance that can be used to manage the background stream. """ flow_control = types.FlowControl(*flow_control) manager = streaming_pull_manager.StreamingPullManager( self, subscription, flow_control=flow_control, scheduler=scheduler, use_legacy_flow_control=use_legacy_flow_control, ) future = futures.StreamingPullFuture(manager) manager.open(callback=callback, on_callback_error=future.set_exception) return future
def subscribe_experimental( self, subscription, callback, flow_control=(), scheduler_=None): """Asynchronously start receiving messages on a given subscription. This method starts a background thread to begin pulling messages from a Pub/Sub subscription and scheduling them to be processed using the provided ``callback``. The ``callback`` will be called with an individual :class:`google.cloud.pubsub_v1.subscriber.message.Message`. It is the responsibility of the callback to either call ``ack()`` or ``nack()`` on the message when it finished processing. If an exception occurs in the callback during processing, the exception is logged and the message is ``nack()`` ed. The ``flow_control`` argument can be used to control the rate of at which messages are pulled. The settings are relatively conservative by default to prevent "message hoarding" - a situation where the client pulls a large number of messages but can not process them fast enough leading it to "starve" other clients of messages. Increasing these settings may lead to faster throughput for messages that do not take a long time to process. This method starts the receiver in the background and returns a *Future* representing its execution. Waiting on the future (calling ``result()``) will block forever or until a non-recoverable error is encountered (such as loss of network connectivity). Cancelling the future will signal the process to shutdown gracefully and exit. Example .. code-block:: python from google.cloud.pubsub_v1 import subscriber subscriber_client = pubsub.SubscriberClient() # existing subscription subscription = subscriber_client.subscription_path( 'my-project-id', 'my-subscription') def callback(message): print(message) message.ack() future = subscriber.subscribe_experimental( subscription, callback) try: future.result() except KeyboardInterrupt: future.cancel() Args: subscription (str): The name of the subscription. The subscription should have already been created (for example, by using :meth:`create_subscription`). callback (Callable[~.pubsub_v1.subscriber.message.Message]): The callback function. This function receives the message as its only argument and will be called from a different thread/ process depending on the scheduling strategy. flow_control (~.pubsub_v1.types.FlowControl): The flow control settings. Use this to prevent situations where you are inundated with too many messages at once. Returns: google.cloud.pubsub_v1.futures.StreamingPullFuture: A Future object that can be used to manage the background stream. """ flow_control = types.FlowControl(*flow_control) manager = streaming_pull_manager.StreamingPullManager( self, subscription, flow_control) future = futures.StreamingPullFuture(manager) manager.open(callback) return future
def make_future(self): manager = mock.create_autospec( streaming_pull_manager.StreamingPullManager, instance=True) future = futures.StreamingPullFuture(manager) return future