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


Python pubsub.PublisherClient方法代碼示例

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


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

示例1: test_topic

# 需要導入模塊: from google.cloud import pubsub [as 別名]
# 或者: from google.cloud.pubsub import PublisherClient [as 別名]
def test_topic():
    pubsub_client = pubsub.PublisherClient()
    try:
        topic = manager.create_iot_topic(project_id, topic_id)
    except AlreadyExists as e:
        print("The topic already exists, detail: {}".format(str(e)))
        # Ignore the error, fetch the topic
        topic = pubsub_client.get_topic(
            pubsub_client.topic_path(project_id, topic_id))

    yield topic

    topic_path = pubsub_client.topic_path(project_id, topic_id)
    try:
        pubsub_client.delete_topic(topic_path)
    except NotFound as e:
        # We ignore this case.
        print("The topic doesn't exist: detail: {}".format(str(e))) 
開發者ID:GoogleCloudPlatform,項目名稱:python-docs-samples,代碼行數:20,代碼來源:fixtures.py

示例2: push_id_to_pubsub

# 需要導入模塊: from google.cloud import pubsub [as 別名]
# 或者: from google.cloud.pubsub import PublisherClient [as 別名]
def push_id_to_pubsub(project: str,
                      publisher_client: google.cloud.pubsub.PublisherClient,
                      job_entry: dict) -> None:
    """Pushes unfinished ID back into PubSub topic until operation is complete.

    Args:
      publisher_client: google.cloud.pubsub.PublisherClient
      project: String representing GCP project ID
      job_entry: Dict holding operation ID and audio file name

    Returns:
      None; Logs message to Stackdriver.
    """
    logging.info(f'Starting push_id_to_pubsub with {project} and {job_entry}')
    topic_name = os.environ.get('topic_name')
    topic_path = publisher_client.topic_path(project, topic_name)
    message = job_entry['id']
    publisher_client.publish(topic_path,
                             message.encode("utf-8"),
                             operation_name=job_entry['id'],
                             audio_file_name=job_entry['file'],
                             pipeline_start_time=job_entry['pipeline_start_time'])
    log_message = (f'Repushed STT {job_entry["id"]} for {job_entry["file"]} to '
                   f'PubSub')
    logging.info(log_message) 
開發者ID:GoogleCloudPlatform,項目名稱:professional-services,代碼行數:27,代碼來源:main.py

示例3: get_client

# 需要導入模塊: from google.cloud import pubsub [as 別名]
# 或者: from google.cloud.pubsub import PublisherClient [as 別名]
def get_client(self):
        if SERVICE_ACCOUNT_JSON:
            json_dict = json.loads(SERVICE_ACCOUNT_JSON)
            LOG.info('using service account JSON : %s', json_dict)
            credential =  service_account.Credentials.from_service_account_info(json_dict)
            scoped_credential = credential.with_scopes(PUBSUB_SCOPES)
            return pubsub.PublisherClient(credentials=scoped_credential)

        LOG.info('default')
        return pubsub.PublisherClient() 
開發者ID:alerta,項目名稱:alerta-contrib,代碼行數:12,代碼來源:alerta_pubsub.py

示例4: __init__

# 需要導入模塊: from google.cloud import pubsub [as 別名]
# 或者: from google.cloud.pubsub import PublisherClient [as 別名]
def __init__(self, logger=None, destination=None, *args, **kwargs):
        import google
        from google.cloud import pubsub, pubsub_v1
        self.logger = logger
        if logger is None:
            self.logger = logging.getLogger('null-logger')
            self.logger.setLevel(9999)
        if destination == "full_ipv4":
            self.topic_url = os.environ.get('PUBSUB_IPV4_TOPIC_URL')
        elif destination == "alexa_top1mil":
            self.topic_url = os.environ.get('PUBSUB_ALEXA_TOPIC_URL')
        self.cert_topic_url = os.environ.get('PUBSUB_CERT_TOPIC_URL')
        if not self.topic_url:
            raise Exception('missing $PUBSUB_[IPV4|ALEXA]_TOPIC_URL')
        if not self.cert_topic_url:
            raise Exception('missing $PUBSUB_CERT_TOPIC_URL')
        batch_settings = pubsub_v1.types.BatchSettings(
            # "The entire request including one or more messages must
            #  be smaller than 10MB, after decoding."
            max_bytes=8192000,  # 8 MB
            max_latency=15,     # 15 seconds
        )
        self.publisher = pubsub.PublisherClient(batch_settings)
        self.publish_count = {}
        try:
            self.publisher.get_topic(self.topic_url)
            self.publisher.get_topic(self.cert_topic_url)
        except google.api_core.exceptions.GoogleAPICallError as e:
            logger.error(e.message)
            raise
        self._state = PubsubState() 
開發者ID:zmap,項目名稱:ztag,代碼行數:33,代碼來源:stream.py

示例5: test_exported_things

# 需要導入模塊: from google.cloud import pubsub [as 別名]
# 或者: from google.cloud.pubsub import PublisherClient [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

示例6: setup_publisher

# 需要導入模塊: from google.cloud import pubsub [as 別名]
# 或者: from google.cloud.pubsub import PublisherClient [as 別名]
def setup_publisher(self):
    """Set up the pubsub publisher."""
    config.LoadConfig()
    self.publisher = pubsub.PublisherClient()
    self.topic_path = self.publisher.topic_path(
        config.TURBINIA_PROJECT, self.topic_name)
    try:
      log.debug('Trying to create pubsub topic {0:s}'.format(self.topic_path))
      self.publisher.create_topic(self.topic_path)
    except exceptions.Conflict:
      log.debug('PubSub topic {0:s} already exists.'.format(self.topic_path))
    log.debug('Setup PubSub publisher at {0:s}'.format(self.topic_path)) 
開發者ID:google,項目名稱:turbinia,代碼行數:14,代碼來源:pubsub.py

示例7: _backend_setup

# 需要導入模塊: from google.cloud import pubsub [as 別名]
# 或者: from google.cloud.pubsub import PublisherClient [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

示例8: test_pubsub

# 需要導入模塊: from google.cloud import pubsub [as 別名]
# 或者: from google.cloud.pubsub import PublisherClient [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

示例9: topic_path

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

示例10: _infinite_publish_job

# 需要導入模塊: from google.cloud import pubsub [as 別名]
# 或者: from google.cloud.pubsub import PublisherClient [as 別名]
def _infinite_publish_job(topic_path):
    publisher_client = pubsub.PublisherClient()
    while True:
        future = publisher_client.publish(
            topic_path,
            b'{"url": "https://beam.apache.org/", "review": "positive"}')
        future.result()
        time.sleep(1) 
開發者ID:GoogleCloudPlatform,項目名稱:python-docs-samples,代碼行數:10,代碼來源:streaming_beam_test.py

示例11: create_iot_topic

# 需要導入模塊: from google.cloud import pubsub [as 別名]
# 或者: from google.cloud.pubsub import PublisherClient [as 別名]
def create_iot_topic(project, topic_name):
    """Creates a PubSub Topic and grants access to Cloud IoT Core."""
    pubsub_client = pubsub.PublisherClient()
    topic_path = pubsub_client.topic_path(project, topic_name)

    topic = pubsub_client.create_topic(topic_path)
    policy = pubsub_client.get_iam_policy(topic_path)

    policy.bindings.add(
        role='roles/pubsub.publisher',
        members=['serviceAccount:cloud-iot@system.gserviceaccount.com'])

    pubsub_client.set_iam_policy(topic_path, policy)

    return topic 
開發者ID:GoogleCloudPlatform,項目名稱:python-docs-samples,代碼行數:17,代碼來源:manager.py

示例12: test_send_to_device

# 需要導入模塊: from google.cloud import pubsub [as 別名]
# 或者: from google.cloud.pubsub import PublisherClient [as 別名]
def test_send_to_device(capsys):
    manager.create_iot_topic(project_id, topic_id)
    manager.open_registry(
        service_account_json,
        project_id,
        cloud_region,
        pubsub_topic,
        registry_id)

    manager.create_unauth_device(
        service_account_json,
        project_id,
        cloud_region,
        registry_id,
        device_id)

    gcs_to_device.send_to_device(
        gcs_bucket,
        gcs_file_name,
        destination_file_name,
        project_id,
        cloud_region,
        registry_id,
        device_id,
        service_account_json)

    manager.delete_device(
        service_account_json, project_id, cloud_region, registry_id,
        device_id)

    manager.delete_registry(
        service_account_json, project_id, cloud_region, registry_id)

    pubsub_client = pubsub.PublisherClient()
    topic_path = pubsub_client.topic_path(project_id, topic_id)
    pubsub_client.delete_topic(topic_path)

    out, _ = capsys.readouterr()
    assert 'Successfully sent file to device' in out 
開發者ID:GoogleCloudPlatform,項目名稱:python-docs-samples,代碼行數:41,代碼來源:gcs_send_to_device_test.py

示例13: test_get_state

# 需要導入模塊: from google.cloud import pubsub [as 別名]
# 或者: from google.cloud.pubsub import PublisherClient [as 別名]
def test_get_state(capsys):
    manager.create_iot_topic(project_id, topic_id)
    manager.open_registry(
        service_account_json,
        project_id,
        cloud_region,
        pubsub_topic,
        registry_id)

    manager.create_unauth_device(
        service_account_json,
        project_id,
        cloud_region,
        registry_id,
        device_id)

    gcs_to_device.get_state(
        service_account_json,
        project_id,
        cloud_region,
        registry_id,
        device_id)

    manager.delete_device(
        service_account_json, project_id, cloud_region, registry_id,
        device_id)

    manager.delete_registry(
        service_account_json, project_id, cloud_region, registry_id)

    pubsub_client = pubsub.PublisherClient()
    topic_path = pubsub_client.topic_path(project_id, topic_id)
    pubsub_client.delete_topic(topic_path)

    out, _ = capsys.readouterr()
    assert 'Id' in out
    assert 'Config' in out 
開發者ID:GoogleCloudPlatform,項目名稱:python-docs-samples,代碼行數:39,代碼來源:gcs_send_to_device_test.py

示例14: test_topic

# 需要導入模塊: from google.cloud import pubsub [as 別名]
# 或者: from google.cloud.pubsub import PublisherClient [as 別名]
def test_topic():
    topic = manager.create_iot_topic(project_id, topic_id)

    yield topic

    pubsub_client = pubsub.PublisherClient()
    topic_path = pubsub_client.topic_path(project_id, topic_id)
    pubsub_client.delete_topic(topic_path) 
開發者ID:GoogleCloudPlatform,項目名稱:python-docs-samples,代碼行數:10,代碼來源:cloudiot_http_example_test.py

示例15: _publish_connect

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


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