def pull(stub, subscription): req = pubsub_pb2.PullRequest(subscription=subscription, max_messages=10) try: resp = stub.Pull(req, TIMEOUT) for t in resp.received_messages: print("Message is: {}".format(t)) except NetworkError, e: logging.warning('Failed to list topics: {}'.format(e)) sys.exit(1)
def pull(self, subscription, max_messages, return_immediately=False, options=None): """ Pulls messages from the server. Returns an empty list if there are no messages available in the backlog. The server may return ``UNAVAILABLE`` if there are too many concurrent pull requests pending for the given subscription. Example: >>> from google.cloud.gapic.pubsub.v1 import subscriber_api >>> api = subscriber_api.SubscriberApi() >>> subscription = api.subscription_path('[PROJECT]', '[SUBSCRIPTION]') >>> max_messages = 0 >>> response = api.pull(subscription, max_messages) Args: subscription (string): The subscription from which messages should be pulled. return_immediately (bool): If this is specified as true the system will respond immediately even if it is not able to return a message in the ``Pull`` response. Otherwise the system is allowed to wait until at least one message is available rather than returning no messages. The client may cancel the request if it does not wish to wait any longer for the response. max_messages (int): The maximum number of messages returned for this request. The Pub/Sub system may return fewer than the number specified. options (:class:`google.gax.CallOptions`): Overrides the default settings for this call, e.g, timeout, retries etc. Returns: A :class:`google.pubsub.v1.pubsub_pb2.PullResponse` instance. Raises: :exc:`google.gax.errors.GaxError` if the RPC is aborted. :exc:`ValueError` if the parameters are invalid. """ request = pubsub_pb2.PullRequest(subscription=subscription, max_messages=max_messages, return_immediately=return_immediately) return self._pull(request, options)