Last active
March 29, 2023 14:31
-
-
Save stefanopassador/d4d9229911a7d9f72c3e627d3ab84f5d to your computer and use it in GitHub Desktop.
Code for Batch Subscriber
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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