本文整理汇总了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)))
示例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)
示例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()
示例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()
示例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
示例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))
示例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)
示例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)
示例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)
示例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)
示例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
示例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
示例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
示例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)
示例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()