Subscriber Client API¶
- class google.cloud.pubsublite.cloudpubsub.subscriber_client.AsyncSubscriberClient(*, nack_handler: Optional[google.cloud.pubsublite.cloudpubsub.nack_handler.NackHandler] = None, reassignment_handler: Optional[google.cloud.pubsublite.cloudpubsub.reassignment_handler.ReassignmentHandler] = None, message_transformer: Optional[google.cloud.pubsublite.cloudpubsub.message_transformer.MessageTransformer] = None, credentials: Optional[google.auth.credentials.Credentials] = None, transport: str = 'grpc_asyncio', client_options: Optional[google.api_core.client_options.ClientOptions] = None)[source]¶
An AsyncSubscriberClient reads messages similar to Google Pub/Sub, but must be used in an async context. Any subscribe failures are unlikely to succeed if retried.
Must be used in an async with block or have __aenter__() awaited before use.
Create a new AsyncSubscriberClient.
- Parameters
nack_handler – A handler for when nack() is called. The default NackHandler raises an exception and fails the subscribe stream.
message_transformer – A transformer from Pub/Sub Lite messages to Cloud Pub/Sub messages. This may not return a message with “message_id” set.
credentials – If provided, the credentials to use when connecting.
transport – The transport to use. Must correspond to an asyncio transport.
client_options – The client options to use when connecting. If used, must explicitly set api_endpoint.
- async __aexit__(exc_type, exc_value, traceback)[source]¶
Raise any exception triggered within the runtime context.
- async subscribe(subscription: Union[google.cloud.pubsublite.types.paths.SubscriptionPath, str], per_partition_flow_control_settings: google.cloud.pubsublite.types.flow_control_settings.FlowControlSettings, fixed_partitions: Optional[Set[google.cloud.pubsublite.types.partition.Partition]] = None) AsyncIterator[google.cloud.pubsub_v1.subscriber.message.Message] [source]¶
Read messages from a subscription.
- Parameters
subscription – The subscription to subscribe to.
per_partition_flow_control_settings – The flow control settings for each partition subscribed to. Note that these settings apply to each partition individually, not in aggregate.
fixed_partitions – A fixed set of partitions to subscribe to. If not present, will instead use auto-assignment.
- Returns
An AsyncIterator with Messages that must have ack() called on each exactly once.
- Raises
GoogleApiCallError – On a permanent failure.
- class google.cloud.pubsublite.cloudpubsub.subscriber_client.SubscriberClient(*, executor: Optional[concurrent.futures.thread.ThreadPoolExecutor] = None, nack_handler: Optional[google.cloud.pubsublite.cloudpubsub.nack_handler.NackHandler] = None, reassignment_handler: Optional[google.cloud.pubsublite.cloudpubsub.reassignment_handler.ReassignmentHandler] = None, message_transformer: Optional[google.cloud.pubsublite.cloudpubsub.message_transformer.MessageTransformer] = None, credentials: Optional[google.auth.credentials.Credentials] = None, transport: str = 'grpc_asyncio', client_options: Optional[google.api_core.client_options.ClientOptions] = None)[source]¶
A SubscriberClient reads messages similar to Google Pub/Sub. Any subscribe failures are unlikely to succeed if retried.
Must be used in a with block or have __enter__() called before use.
Create a new SubscriberClient.
- Parameters
executor – A ThreadPoolExecutor to use. The client will shut it down on __exit__. If provided a single threaded executor, messages will be ordered per-partition, but take care that the callback does not block for too long as it will impede forward progress on all subscriptions.
nack_handler – A handler for when nack() is called. The default NackHandler raises an exception and fails the subscribe stream.
message_transformer – A transformer from Pub/Sub Lite messages to Cloud Pub/Sub messages. This may not return a message with “message_id” set.
credentials – If provided, the credentials to use when connecting.
transport – The transport to use. Must correspond to an asyncio transport.
client_options – The client options to use when connecting. If used, must explicitly set api_endpoint.
- __exit__(exc_type, exc_value, traceback)[source]¶
Raise any exception triggered within the runtime context.
- subscribe(subscription: Union[google.cloud.pubsublite.types.paths.SubscriptionPath, str], callback: Callable[[google.cloud.pubsub_v1.subscriber.message.Message], None], per_partition_flow_control_settings: google.cloud.pubsublite.types.flow_control_settings.FlowControlSettings, fixed_partitions: Optional[Set[google.cloud.pubsublite.types.partition.Partition]] = None) google.cloud.pubsub_v1.subscriber.futures.StreamingPullFuture [source]¶
This method starts a background thread to begin pulling messages from a Pub/Sub Lite subscription and scheduling them to be processed using the provided
callback
.- Parameters
subscription – The subscription to subscribe to.
callback – The callback function. This function receives the message as its only argument.
per_partition_flow_control_settings – The flow control settings for each partition subscribed to. Note that these settings apply to each partition individually, not in aggregate.
fixed_partitions – A fixed set of partitions to subscribe to. If not present, will instead use auto-assignment.
- Returns
A StreamingPullFuture instance that can be used to manage the background stream.
- Raises
GoogleApiCallError – On a permanent failure.