Skip to content

Instantly share code, notes, and snippets.

@stefanopassador
Last active March 29, 2023 14:31
Show Gist options
  • Save stefanopassador/d4d9229911a7d9f72c3e627d3ab84f5d to your computer and use it in GitHub Desktop.
Save stefanopassador/d4d9229911a7d9f72c3e627d3ab84f5d to your computer and use it in GitHub Desktop.
Code for Batch Subscriber
import os
import time
import random
import json
from google.cloud import pubsub_v1
## GCP config
PROJECT_ID = 'streaming-ingestion-XXXXXX'
TOPIC_ID = 'sample-topic'
SUBSCRIPTION_ID = 'sample-topic-sub'
SERVICE_ACCOUNT_PATH = './batch-subscription-service-account.json'
subscription_name = f'projects/{PROJECT_ID}/subscriptions/{SUBSCRIPTION_ID}'
def callback(message):
print(message.data)
message.ack()
if __name__ == '__main__':
with pubsub_v1.SubscriberClient.from_service_account_json(SERVICE_ACCOUNT_PATH) as subscriber:
future = subscriber.subscribe(subscription_name, callback)
try:
future.result()
except KeyboardInterrupt:
future.cancel()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment