本文整理汇总了Python中google.cloud.pubsub_v1.SubscriberClient方法的典型用法代码示例。如果您正苦于以下问题:Python pubsub_v1.SubscriberClient方法的具体用法?Python pubsub_v1.SubscriberClient怎么用?Python pubsub_v1.SubscriberClient使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类google.cloud.pubsub_v1
的用法示例。
在下文中一共展示了pubsub_v1.SubscriberClient方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: list_subscriptions_in_project
# 需要导入模块: from google.cloud import pubsub_v1 [as 别名]
# 或者: from google.cloud.pubsub_v1 import SubscriberClient [as 别名]
def list_subscriptions_in_project(project_id):
"""Lists all subscriptions in the current project."""
# [START pubsub_list_subscriptions]
from google.cloud import pubsub_v1
# TODO(developer)
# project_id = "your-project-id"
subscriber = pubsub_v1.SubscriberClient()
project_path = subscriber.project_path(project_id)
# Wrap the subscriber in a 'with' block to automatically call close() to
# close the underlying gRPC channel when done.
with subscriber:
for subscription in subscriber.list_subscriptions(project_path):
print(subscription.name)
# [END pubsub_list_subscriptions]
示例2: create_subscription
# 需要导入模块: from google.cloud import pubsub_v1 [as 别名]
# 或者: from google.cloud.pubsub_v1 import SubscriberClient [as 别名]
def create_subscription(project_id, topic_id, subscription_id):
"""Create a new pull subscription on the given topic."""
# [START pubsub_create_pull_subscription]
from google.cloud import pubsub_v1
# TODO(developer)
# project_id = "your-project-id"
# topic_id = "your-topic-id"
# subscription_id = "your-subscription-id"
subscriber = pubsub_v1.SubscriberClient()
topic_path = subscriber.topic_path(project_id, topic_id)
subscription_path = subscriber.subscription_path(project_id, subscription_id)
# Wrap the subscriber in a 'with' block to automatically call close() to
# close the underlying gRPC channel when done.
with subscriber:
subscription = subscriber.create_subscription(subscription_path, topic_path)
print("Subscription created: {}".format(subscription))
# [END pubsub_create_pull_subscription]
示例3: get_subscription_policy
# 需要导入模块: from google.cloud import pubsub_v1 [as 别名]
# 或者: from google.cloud.pubsub_v1 import SubscriberClient [as 别名]
def get_subscription_policy(project, subscription_id):
"""Prints the IAM policy for the given subscription."""
# [START pubsub_get_subscription_policy]
from google.cloud import pubsub_v1
# TODO(developer)
# project_id = "your-project-id"
# subscription_id = "your-subscription-id"
client = pubsub_v1.SubscriberClient()
subscription_path = client.subscription_path(project, subscription_id)
policy = client.get_iam_policy(subscription_path)
print("Policy for subscription {}:".format(subscription_path))
for binding in policy.bindings:
print("Role: {}, Members: {}".format(binding.role, binding.members))
client.close()
# [END pubsub_get_subscription_policy]
示例4: poll_notifications
# 需要导入模块: from google.cloud import pubsub_v1 [as 别名]
# 或者: from google.cloud.pubsub_v1 import SubscriberClient [as 别名]
def poll_notifications(project, subscription_name):
"""Polls a Cloud Pub/Sub subscription for new GCS events for display."""
# [BEGIN poll_notifications]
subscriber = pubsub_v1.SubscriberClient()
subscription_path = subscriber.subscription_path(
project, subscription_name
)
def callback(message):
print("Received message:\n{}".format(summarize(message)))
message.ack()
subscriber.subscribe(subscription_path, callback=callback)
# The subscriber is non-blocking, so we must keep the main thread from
# exiting to allow it to process messages in the background.
print("Listening for messages on {}".format(subscription_path))
while True:
time.sleep(60)
# [END poll_notifications]
示例5: receive_messages
# 需要导入模块: from google.cloud import pubsub_v1 [as 别名]
# 或者: from google.cloud.pubsub_v1 import SubscriberClient [as 别名]
def receive_messages():
"""Receives messages from a pull subscription."""
scopes = ['https://www.googleapis.com/auth/chat.bot']
credentials, project_id = google.auth.default()
credentials = credentials.with_scopes(scopes=scopes)
chat = build('chat', 'v1', credentials=credentials)
subscription_id = os.environ.get('SUBSCRIPTION_ID')
subscriber = pubsub_v1.SubscriberClient()
subscription_path = subscriber.subscription_path(
project_id, subscription_id)
def callback(message):
logging.info('Received message: %s', message.data)
event = json.loads(message.data)
space_name = event['space']['name']
# If the bot was removed, we don't need to return a response.
if event['type'] == 'REMOVED_FROM_SPACE':
logging.info('Bot removed rom space %s', space_name)
return
response = format_response(event)
# Send the asynchronous response back to Hangouts Chat
chat.spaces().messages().create(
parent=space_name,
body=response).execute()
message.ack()
subscriber.subscribe(subscription_path, callback=callback)
logging.info('Listening for messages on %s', subscription_path)
# Keep main thread from exiting while waiting for messages
while True:
time.sleep(60)
示例6: __init__
# 需要导入模块: from google.cloud import pubsub_v1 [as 别名]
# 或者: from google.cloud.pubsub_v1 import SubscriberClient [as 别名]
def __init__(self, gc_project_id=None, credentials=None, default_ack_deadline=None):
if gc_project_id is None or credentials is None:
creds, project = get_google_defaults()
self._gc_project_id = gc_project_id or project
self._ack_deadline = default_ack_deadline or DEFAULT_ACK_DEADLINE
_credentials = credentials or creds
if USE_EMULATOR:
self._client = pubsub_v1.SubscriberClient()
else:
self._client = pubsub_v1.SubscriberClient(credentials=_credentials)
示例7: consume
# 需要导入模块: from google.cloud import pubsub_v1 [as 别名]
# 或者: from google.cloud.pubsub_v1 import SubscriberClient [as 别名]
def consume(self, subscription_name, callback, scheduler):
"""Begin listening to topic from the SubscriberClient.
:param subscription_name: str Subscription name
:param callback: Function which act on a topic message
:param scheduler: `Thread pool-based scheduler.<https://googleapis.dev/python/pubsub/latest/subscriber/api/scheduler.html?highlight=threadscheduler#google.cloud.pubsub_v1.subscriber.scheduler.ThreadScheduler>`_ # noqa
:return: `Future <https://googleapis.github.io/google-cloud-python/latest/pubsub/subscriber/api/futures.html>`_ # noqa
"""
subscription_path = self._client.subscription_path(
self._gc_project_id, subscription_name
)
return self._client.subscribe(
subscription_path, callback=callback, scheduler=scheduler
)
示例8: subscriber
# 需要导入模块: from google.cloud import pubsub_v1 [as 别名]
# 或者: from google.cloud.pubsub_v1 import SubscriberClient [as 别名]
def subscriber():
yield pubsub_v1.SubscriberClient()
示例9: test_exported_things
# 需要导入模块: from google.cloud import pubsub_v1 [as 别名]
# 或者: from google.cloud.pubsub_v1 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
示例10: subscriber_client
# 需要导入模块: from google.cloud import pubsub_v1 [as 别名]
# 或者: from google.cloud.pubsub_v1 import SubscriberClient [as 别名]
def subscriber_client():
subscriber_client = pubsub_v1.SubscriberClient()
yield subscriber_client
subscriber_client.close()
示例11: sub
# 需要导入模块: from google.cloud import pubsub_v1 [as 别名]
# 或者: from google.cloud.pubsub_v1 import SubscriberClient [as 别名]
def sub(project_id, subscription_id):
"""Receives messages from a Pub/Sub subscription."""
# [START pubsub_quickstart_sub_client]
# Initialize a Subscriber client
subscriber_client = pubsub_v1.SubscriberClient()
# [END pubsub_quickstart_sub_client]
# Create a fully qualified identifier in the form of
# `projects/{project_id}/subscriptions/{subscription_id}`
subscription_path = subscriber_client.subscription_path(project_id, subscription_id)
def callback(message):
print(
"Received message {} of message ID {}\n".format(message, message.message_id)
)
# Acknowledge the message. Unack'ed messages will be redelivered.
message.ack()
print("Acknowledged message {}\n".format(message.message_id))
streaming_pull_future = subscriber_client.subscribe(
subscription_path, callback=callback
)
print("Listening for messages on {}..\n".format(subscription_path))
try:
# Calling result() on StreamingPullFuture keeps the main thread from
# exiting while messages get processed in the callbacks.
streaming_pull_future.result()
except: # noqa
streaming_pull_future.cancel()
subscriber_client.close()
示例12: create_push_subscription
# 需要导入模块: from google.cloud import pubsub_v1 [as 别名]
# 或者: from google.cloud.pubsub_v1 import SubscriberClient [as 别名]
def create_push_subscription(project_id, topic_id, subscription_id, endpoint):
"""Create a new push subscription on the given topic."""
# [START pubsub_create_push_subscription]
from google.cloud import pubsub_v1
# TODO(developer)
# project_id = "your-project-id"
# topic_id = "your-topic-id"
# subscription_id = "your-subscription-id"
# endpoint = "https://my-test-project.appspot.com/push"
subscriber = pubsub_v1.SubscriberClient()
topic_path = subscriber.topic_path(project_id, topic_id)
subscription_path = subscriber.subscription_path(project_id, subscription_id)
push_config = pubsub_v1.types.PushConfig(push_endpoint=endpoint)
# Wrap the subscriber in a 'with' block to automatically call close() to
# close the underlying gRPC channel when done.
with subscriber:
subscription = subscriber.create_subscription(
subscription_path, topic_path, push_config
)
print("Push subscription created: {}".format(subscription))
print("Endpoint for subscription is: {}".format(endpoint))
# [END pubsub_create_push_subscription]
示例13: update_push_subscription
# 需要导入模块: from google.cloud import pubsub_v1 [as 别名]
# 或者: from google.cloud.pubsub_v1 import SubscriberClient [as 别名]
def update_push_subscription(project_id, topic_id, subscription_id, endpoint):
"""
Updates an existing Pub/Sub subscription's push endpoint URL.
Note that certain properties of a subscription, such as
its topic, are not modifiable.
"""
# [START pubsub_update_push_configuration]
from google.cloud import pubsub_v1
# TODO(developer)
# project_id = "your-project-id"
# topic_id = "your-topic-id"
# subscription_id = "your-subscription-id"
# endpoint = "https://my-test-project.appspot.com/push"
subscriber = pubsub_v1.SubscriberClient()
subscription_path = subscriber.subscription_path(project_id, subscription_id)
push_config = pubsub_v1.types.PushConfig(push_endpoint=endpoint)
subscription = pubsub_v1.types.Subscription(
name=subscription_path, topic=topic_id, push_config=push_config
)
update_mask = {"paths": {"push_config"}}
# Wrap the subscriber in a 'with' block to automatically call close() to
# close the underlying gRPC channel when done.
with subscriber:
result = subscriber.update_subscription(subscription, update_mask)
print("Subscription updated: {}".format(subscription_path))
print("New endpoint for subscription is: {}".format(result.push_config))
# [END pubsub_update_push_configuration]
示例14: receive_messages
# 需要导入模块: from google.cloud import pubsub_v1 [as 别名]
# 或者: from google.cloud.pubsub_v1 import SubscriberClient [as 别名]
def receive_messages(project_id, subscription_id, timeout=None):
"""Receives messages from a pull subscription."""
# [START pubsub_subscriber_async_pull]
# [START pubsub_quickstart_subscriber]
from concurrent.futures import TimeoutError
from google.cloud import pubsub_v1
# TODO(developer)
# project_id = "your-project-id"
# subscription_id = "your-subscription-id"
# Number of seconds the subscriber should listen for messages
# timeout = 5.0
subscriber = pubsub_v1.SubscriberClient()
# The `subscription_path` method creates a fully qualified identifier
# in the form `projects/{project_id}/subscriptions/{subscription_id}`
subscription_path = subscriber.subscription_path(project_id, subscription_id)
def callback(message):
print("Received message: {}".format(message))
message.ack()
streaming_pull_future = subscriber.subscribe(subscription_path, callback=callback)
print("Listening for messages on {}..\n".format(subscription_path))
# Wrap subscriber in a 'with' block to automatically call close() when done.
with subscriber:
try:
# When `timeout` is not set, result() will block indefinitely,
# unless an exception is encountered first.
streaming_pull_future.result(timeout=timeout)
except TimeoutError:
streaming_pull_future.cancel()
# [END pubsub_subscriber_async_pull]
# [END pubsub_quickstart_subscriber]
示例15: receive_messages_with_custom_attributes
# 需要导入模块: from google.cloud import pubsub_v1 [as 别名]
# 或者: from google.cloud.pubsub_v1 import SubscriberClient [as 别名]
def receive_messages_with_custom_attributes(project_id, subscription_id, timeout=None):
"""Receives messages from a pull subscription."""
# [START pubsub_subscriber_async_pull_custom_attributes]
from concurrent.futures import TimeoutError
from google.cloud import pubsub_v1
# TODO(developer)
# project_id = "your-project-id"
# subscription_id = "your-subscription-id"
# Number of seconds the subscriber should listen for messages
# timeout = 5.0
subscriber = pubsub_v1.SubscriberClient()
subscription_path = subscriber.subscription_path(project_id, subscription_id)
def callback(message):
print("Received message: {}".format(message.data))
if message.attributes:
print("Attributes:")
for key in message.attributes:
value = message.attributes.get(key)
print("{}: {}".format(key, value))
message.ack()
streaming_pull_future = subscriber.subscribe(subscription_path, callback=callback)
print("Listening for messages on {}..\n".format(subscription_path))
# Wrap subscriber in a 'with' block to automatically call close() when done.
with subscriber:
try:
# When `timeout` is not set, result() will block indefinitely,
# unless an exception is encountered first.
streaming_pull_future.result(timeout=timeout)
except TimeoutError:
streaming_pull_future.cancel()
# [END pubsub_subscriber_async_pull_custom_attributes]