當前位置: 首頁>>代碼示例>>Python>>正文


Python pubsub.SubscriberClient方法代碼示例

本文整理匯總了Python中google.cloud.pubsub.SubscriberClient方法的典型用法代碼示例。如果您正苦於以下問題:Python pubsub.SubscriberClient方法的具體用法?Python pubsub.SubscriberClient怎麽用?Python pubsub.SubscriberClient使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在google.cloud.pubsub的用法示例。


在下文中一共展示了pubsub.SubscriberClient方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: setup_subscriber

# 需要導入模塊: from google.cloud import pubsub [as 別名]
# 或者: from google.cloud.pubsub import SubscriberClient [as 別名]
def setup_subscriber(self):
    """Set up the pubsub subscriber."""
    config.LoadConfig()
    self.subscriber = pubsub.SubscriberClient()
    subscription_path = self.subscriber.subscription_path(
        config.TURBINIA_PROJECT, self.topic_name)
    if not self.topic_path:
      self.topic_path = self.subscriber.topic_path(
          config.TURBINIA_PROJECT, self.topic_name)
    try:
      log.debug(
          'Trying to create subscription {0:s} on topic {1:s}'.format(
              subscription_path, self.topic_path))
      self.subscriber.create_subscription(subscription_path, self.topic_path)
    except exceptions.Conflict:
      log.debug('Subscription {0:s} already exists.'.format(subscription_path))

    log.debug('Setup PubSub Subscription {0:s}'.format(subscription_path))
    self.subscription = self.subscriber.subscribe(
        subscription_path, self._callback) 
開發者ID:google,項目名稱:turbinia,代碼行數:22,代碼來源:pubsub.py

示例2: pubsub

# 需要導入模塊: from google.cloud import pubsub [as 別名]
# 或者: from google.cloud.pubsub import SubscriberClient [as 別名]
def pubsub(subscription_id, timeout_seconds, project_id):
    """Respond to incoming occurrences using a Cloud Pub/Sub subscription."""
    # subscription_id := 'my-occurrences-subscription'
    # timeout_seconds = 20
    # project_id = 'my-gcp-project'

    import time
    from google.cloud.pubsub import SubscriberClient

    client = SubscriberClient()
    subscription_name = client.subscription_path(project_id, subscription_id)
    receiver = MessageReceiver()
    client.subscribe(subscription_name, receiver.pubsub_callback)

    # listen for 'timeout' seconds
    for _ in range(timeout_seconds):
        time.sleep(1)
    # print and return the number of pubsub messages received
    print(receiver.msg_count)
    return receiver.msg_count 
開發者ID:GoogleCloudPlatform,項目名稱:python-docs-samples,代碼行數:22,代碼來源:samples.py

示例3: check_subscription_exists

# 需要導入模塊: from google.cloud import pubsub [as 別名]
# 或者: from google.cloud.pubsub import SubscriberClient [as 別名]
def check_subscription_exists(project: str,
                              subscriber_client: google.cloud.pubsub.SubscriberClient,
                              subscription_path: str) -> bool:
    """Check if PubSub subscription exists.

    Args:
        subscriber_client: google.cloud.pubsub.SubscriberClient
        project: String of GCP project id
        subscription_path: String representing topic path in format
            project/{proj}/subscriptions/{subscription}

    Returns:
        Boolean denoting if subscription to topic exists.
    """
    logging.info(f'Checking if subscription {subscription_path} exists.')
    project_path = subscriber_client.project_path(project)
    subscription_list = list(subscriber_client.list_subscriptions(project_path))
    return subscription_path in [sub.name for sub in subscription_list] 
開發者ID:GoogleCloudPlatform,項目名稱:professional-services,代碼行數:20,代碼來源:main.py

示例4: test_exported_things

# 需要導入模塊: from google.cloud import pubsub [as 別名]
# 或者: from google.cloud.pubsub import SubscriberClient [as 別名]
def test_exported_things():
    assert pubsub.PublisherClient is pubsub_v1.PublisherClient
    assert pubsub.SubscriberClient is pubsub_v1.SubscriberClient
    assert pubsub.types is pubsub_v1.types 
開發者ID:googleapis,項目名稱:python-pubsub,代碼行數:6,代碼來源:test_pubsub.py

示例5: _backend_setup

# 需要導入模塊: from google.cloud import pubsub [as 別名]
# 或者: from google.cloud.pubsub import SubscriberClient [as 別名]
def _backend_setup(self, server=True, *args, **kwargs):
    """
    Args:
      server (bool): Whether this is the client or a server

    Raises:
      TurbiniaException: When there are errors creating PSQ Queue
    """

    log.debug(
        'Setting up PSQ Task Manager requirements on project {0:s}'.format(
            config.TURBINIA_PROJECT))
    self.server_pubsub = turbinia_pubsub.TurbiniaPubSub(config.PUBSUB_TOPIC)
    if server:
      self.server_pubsub.setup_subscriber()
    else:
      self.server_pubsub.setup_publisher()
    psq_publisher = pubsub.PublisherClient()
    psq_subscriber = pubsub.SubscriberClient()
    datastore_client = datastore.Client(project=config.TURBINIA_PROJECT)
    try:
      self.psq = psq.Queue(
          psq_publisher, psq_subscriber, config.TURBINIA_PROJECT,
          name=config.PSQ_TOPIC, storage=psq.DatastoreStorage(datastore_client))
    except exceptions.GoogleCloudError as e:
      msg = 'Error creating PSQ Queue: {0:s}'.format(str(e))
      log.error(msg)
      raise turbinia.TurbiniaException(msg) 
開發者ID:google,項目名稱:turbinia,代碼行數:30,代碼來源:task_manager.py

示例6: test_pubsub

# 需要導入模塊: from google.cloud import pubsub [as 別名]
# 或者: from google.cloud.pubsub import SubscriberClient [as 別名]
def test_pubsub(self):
        # create topic if needed
        client = SubscriberClient()
        try:
            topic_id = 'container-analysis-occurrences-v1'
            topic_name = client.topic_path(PROJECT_ID, topic_id)
            publisher = PublisherClient()
            publisher.create_topic(topic_name)
        except AlreadyExists:
            pass

        subscription_id = 'container-analysis-test-{}'.format(uuid.uuid4())
        subscription_name = client.subscription_path(PROJECT_ID,
                                                     subscription_id)
        samples.create_occurrence_subscription(subscription_id, PROJECT_ID)

        # I can not make it pass with multiple messages. My guess is
        # the server started to dedup?
        message_count = 1
        try:
            job_done = threading.Event()
            receiver = MessageReceiver(message_count, job_done)
            client.subscribe(subscription_name, receiver.pubsub_callback)

            for i in range(message_count):
                occ = samples.create_occurrence(
                    self.image_url, self.note_id, PROJECT_ID, PROJECT_ID)
                time.sleep(SLEEP_TIME)
                samples.delete_occurrence(basename(occ.name), PROJECT_ID)
                time.sleep(SLEEP_TIME)
            # We saw occational failure with 60 seconds timeout, so we bumped it
            # to 180 seconds.
            # See also: python-docs-samples/issues/2894
            job_done.wait(timeout=180)
            print('done. msg_count = {}'.format(receiver.msg_count))
            assert message_count <= receiver.msg_count
        finally:
            # clean up
            client.delete_subscription(subscription_name) 
開發者ID:GoogleCloudPlatform,項目名稱:python-docs-samples,代碼行數:41,代碼來源:samples_test.py

示例7: create_occurrence_subscription

# 需要導入模塊: from google.cloud import pubsub [as 別名]
# 或者: from google.cloud.pubsub import SubscriberClient [as 別名]
def create_occurrence_subscription(subscription_id, project_id):
    """Creates a new Pub/Sub subscription object listening to the
    Container Analysis Occurrences topic."""
    # subscription_id := 'my-occurrences-subscription'
    # project_id = 'my-gcp-project'

    from google.api_core.exceptions import AlreadyExists
    from google.cloud.pubsub import SubscriberClient

    topic_id = 'container-analysis-occurrences-v1'
    client = SubscriberClient()
    topic_name = client.topic_path(project_id, topic_id)
    subscription_name = client.subscription_path(project_id, subscription_id)
    success = True
    try:
        client.create_subscription(subscription_name, topic_name)
    except AlreadyExists:
        # if subscription already exists, do nothing
        pass
    else:
        success = False
    return success
# [END containeranalysis_pubsub]


# [START containeranalysis_poll_discovery_occurrence_finished] 
開發者ID:GoogleCloudPlatform,項目名稱:python-docs-samples,代碼行數:28,代碼來源:samples.py

示例8: subscription_path

# 需要導入模塊: from google.cloud import pubsub [as 別名]
# 或者: from google.cloud.pubsub import SubscriberClient [as 別名]
def subscription_path(topic_path):
    subscriber = pubsub.SubscriberClient()
    subscription_path = subscriber.subscription_path(PROJECT, SUBSCRIPTION)
    try:
        subscriber.delete_subscription(subscription_path)
    except Exception:
        pass
    subscription = subscriber.create_subscription(subscription_path, topic_path)
    yield subscription.name
    subscriber.delete_subscription(subscription_path) 
開發者ID:GoogleCloudPlatform,項目名稱:python-docs-samples,代碼行數:12,代碼來源:streaming_beam_test.py

示例9: receive_image

# 需要導入模塊: from google.cloud import pubsub [as 別名]
# 或者: from google.cloud.pubsub import SubscriberClient [as 別名]
def receive_image(project_id, subscription_path, prefix, extension, timeout):
    """Receieve images transmitted to a PubSub subscription."""
    subscriber = pubsub.SubscriberClient()

    global count
    count = 0
    file_pattern = '{}-{}.{}'

    # Set up a callback to acknowledge a message. This closes around an event
    # so that it can signal that it is done and the main thread can continue.
    job_done = threading.Event()

    def callback(message):
        global count
        try:
            count = count + 1
            print('Received image {}:'.format(count))
            image_data = base64.b64decode(message.data)

            with io.open(
                    file_pattern.format(prefix, count, extension), 'wb') as f:
                f.write(image_data)
                message.ack()
                # Signal to the main thread that we can exit.
                job_done.set()

        except binascii.Error:
            message.ack()  # To move forward if a message can't be processed

    subscriber.subscribe(subscription_path, callback=callback)

    print('Listening for messages on {}'.format(subscription_path))
    finished = job_done.wait(timeout=timeout)
    if not finished:
        print("No event received before the timeout.") 
開發者ID:GoogleCloudPlatform,項目名稱:python-docs-samples,代碼行數:37,代碼來源:cloudiot_mqtt_image.py

示例10: _ingest_connect

# 需要導入模塊: from google.cloud import pubsub [as 別名]
# 或者: from google.cloud.pubsub import SubscriberClient [as 別名]
def _ingest_connect(self, subscription: str, topic: str) -> None:
        self._publish_connect(topic)
        if not self.ingest_client:
            self.ingest_client = pubsub.SubscriberClient() 
開發者ID:PUNCH-Cyber,項目名稱:stoq-plugins-public,代碼行數:6,代碼來源:pubsub.py

示例11: run

# 需要導入模塊: from google.cloud import pubsub [as 別名]
# 或者: from google.cloud.pubsub import SubscriberClient [as 別名]
def run(self, project_id, pubsub_topic, pubsub_subscription, service_account_json):
      """The main loop for the device. Consume messages from the Pub/Sub topic."""
      pubsub_client = pubsub.SubscriberClient()
      topic_name = 'projects/{}/topics/{}'.format(project_id, pubsub_topic)
      subscription_path = pubsub_client.subscription_path(project_id, pubsub_subscription)
      print 'Server running. Consuming telemetry events from', topic_name

      def callback(message):
          print 'waiting'
        # Pull from the subscription, waiting until there are messages.
          # print '.'
          data = json.loads(message.data)
          # Get the registry id and device id from the attributes. These are
          # automatically supplied by IoT, and allow the server to determine which
          # device sent the event.
          device_project_id = message.attributes['projectId']
          device_registry_id = message.attributes['deviceRegistryId']
          # device_id = message.attributes['deviceId']
          device_region = message.attributes['deviceRegistryLocation']

          devices = list_devices(service_account_json, device_project_id, device_region, device_registry_id)
          for device in devices:
            device_id = device.get('id')

            # don't send to the joystick device
            if (message.attributes['deviceId'] != device_id):
              # Send the config to the device.
              self._update_device_config(device_project_id, device_region, device_registry_id, device_id, data)
          time.sleep(1)
          messesage.ack()
          
      pubsub_client.subscribe(subscription_path, callback=callback)
      
      while(True):
          time.sleep(60)
          #subscription.acknowledge([ack_id for ack_id, message in results]) 
開發者ID:ARM-software,項目名稱:Cloud-IoT-Core-Kit-Examples,代碼行數:38,代碼來源:server_relay.py

示例12: run

# 需要導入模塊: from google.cloud import pubsub [as 別名]
# 或者: from google.cloud.pubsub import SubscriberClient [as 別名]
def run(self, project_id, pubsub_topic, pubsub_subscription, fan_on_thresh, fan_off_thresh):
      """The main loop for the device. Consume messages from the Pub/Sub topic."""
      pubsub_client = pubsub.SubscriberClient()
      topic_name = 'projects/{}/topics/{}'.format(project_id, pubsub_topic)
      #topic = pubsub_client.topic(topic_name)
      subscription_path = pubsub_client.subscription_path(project_id, pubsub_subscription)
      print 'Server running. Consuming telemetry events from', topic_name

      def callback(message):
        # Pull from the subscription, waiting until there are messages..'
          data = json.loads(message.data)
          # Get the registry id and device id from the attributes. These are
          # automatically supplied by IoT, and allow the server to determine which
          # device sent the event.
          device_project_id = message.attributes['projectId']
          device_registry_id = message.attributes['deviceRegistryId']
          device_id = message.attributes['deviceId']
          device_region = message.attributes['deviceRegistryLocation']

          # Send the config to the device.
          self._update_device_config(device_project_id, device_region, device_registry_id, device_id, data, fan_on_thresh, fan_off_thresh)
          # state change updates throttled to 1 sec by pubsub. Obey or crash. 
          time.sleep(1)
          messesage.ack()
          
      pubsub_client.subscribe(subscription_path, callback=callback)
      
      while(True):
          time.sleep(60) 
開發者ID:ARM-software,項目名稱:Cloud-IoT-Core-Kit-Examples,代碼行數:31,代碼來源:control_server.py

示例13: get_received_msgs_from_pubsub

# 需要導入模塊: from google.cloud import pubsub [as 別名]
# 或者: from google.cloud.pubsub import SubscriberClient [as 別名]
def get_received_msgs_from_pubsub(subscriber_client: google.cloud.pubsub.SubscriberClient,
                                  project: str) -> list:
    """Retrieves receives messages in PubSub topic.

    Args:
        subscriber_client: google.cloud.pubsub.SubscriberClient
        project: String holding GCP project id

    Returns:
        Array of PubSub messages holding message metadata
    """
    logging.info('Starting get_received_msgs_from_pubsub')
    try:
        subscription_name = os.environ.get('subscription_name')
        topic_name = os.environ.get('topic_name')
        topic_path = subscriber_client.topic_path(project, topic_name)
        subscription_path = subscriber_client.subscription_path(project,
                                                                subscription_name)
        if not check_subscription_exists(project, subscriber_client,
                                         subscription_path):
            logging.info(f'Subscription {subscription_path} does not exist yet.'
                         f' Creating now.')
            subscriber_client.create_subscription(subscription_path, topic_path)
        response = subscriber_client.pull(subscription_path, max_messages=50)
        return list(response.received_messages)
    except Exception as e:
        logging.error(e) 
開發者ID:GoogleCloudPlatform,項目名稱:professional-services,代碼行數:29,代碼來源:main.py

示例14: get_stt_ids_from_msgs

# 需要導入模塊: from google.cloud import pubsub [as 別名]
# 或者: from google.cloud.pubsub import SubscriberClient [as 別名]
def get_stt_ids_from_msgs(subscriber_client: google.cloud.pubsub.SubscriberClient,
                          project: str,
                          received_messages: list) -> list:
    """Creates array of objects with STT API operation IDs and file names.

    Args:
        subscriber_client: google.cloud.pubsub.SubscriberClient
        project: String holding GCP project id
        received_messages: List of PubSub message objects

    Returns:
        Array of objects in format [{'file': file1,
                                    'id': id1}]
    """
    logging.info('Starting to get_stt_ids_from_msgs')
    try:
        file_id_map = [{'file': msg.message.attributes.get('audio_file_name'),
                        'id': msg.message.attributes.get('operation_name'),
                        'pipeline_start_time': msg.message.attributes.get('pipeline_start_time')}
                       for msg in received_messages]
        if received_messages:
            ack_ids = [msg.ack_id for msg in received_messages]
            subscription_name = os.environ.get('subscription_name')
            subscription_path = subscriber_client.subscription_path(project,
                                                                    subscription_name)
            subscriber_client.acknowledge(subscription_path, ack_ids)
        else:
            logging.info('No new messages were found in PubSub queue.')
        return file_id_map

    except Exception as e:
        logging.error('Getting STT ids from PubSub received messages failed.')
        logging.error(e) 
開發者ID:GoogleCloudPlatform,項目名稱:professional-services,代碼行數:35,代碼來源:main.py

示例15: subscriber

# 需要導入模塊: from google.cloud import pubsub [as 別名]
# 或者: from google.cloud.pubsub import SubscriberClient [as 別名]
def subscriber():
    subscriber = pubsub.SubscriberClient()
    subscriber.create_subscription(
        SUBSCRIPTION_NAME, TOPIC_NAME
    )
    yield subscriber
    subscriber.delete_subscription(SUBSCRIPTION_NAME) 
開發者ID:GoogleCloudPlatform,項目名稱:getting-started-python,代碼行數:9,代碼來源:main_test.py


注:本文中的google.cloud.pubsub.SubscriberClient方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。