当前位置: 首页>>代码示例>>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;未经允许,请勿转载。