def __get_publisher(self): publisher = pubsub_v1.PublisherClient( credentials=self.__get_credentials(), batch_settings=types.BatchSettings(max_messages=QQUEUE_MAX_BATCH_LIMIT) ) return ( publisher, publisher.topic_path(self.project_id, self.queue_name) )
def __init__(self): super(StreamListener, self).__init__() import json from google.auth import jwt service_account_info = json.load(open("configs/hpcnt-practice.json")) publisher_audience = "https://pubsub.googleapis.com/google.pubsub.v1.Publisher" credentials = jwt.Credentials.from_service_account_info( service_account_info, audience=publisher_audience ) self.publisher = pubsub.PublisherClient( batch_settings=types.BatchSettings(max_messages=500), credentials = credentials )
def publish_messages(project_id, topic_name): from google.cloud import pubsub_v1 from google.cloud.pubsub import types import datetime publisher = pubsub_v1.PublisherClient(batch_settings=types.BatchSettings( max_messages=10)) topic_path = publisher.topic_path(project_id, topic_name) aws = [] for n in range(1, 50): date = datetime.datetime.now().isoformat() data = u"Message number {}".format(n) data = data.encode('utf-8') # future = publisher.publish(topic_path, data=data) future = publish(publisher, topic_path, data) # aw = ensure_future(future) # aws.append(aw) print("[{}]".format(date), future.result()) # await asyncio.gather(*aws) print("Published messages.")
def __init__(self): super(StreamListener, self).__init__() self.publisher = pubsub.PublisherClient( batch_settings=types.BatchSettings(max_messages=500))
from time import sleep from threading import Timer import calendar import time PROJECT_ID = 'hpcnt-practice' PUBSUB_TOPIC = 'hpcnt-tutorial-file' service_account_info = json.load(open("configs/hpcnt-practice.json")) publisher_audience = "https://pubsub.googleapis.com/google.pubsub.v1.Publisher" credentials = jwt.Credentials.from_service_account_info( service_account_info, audience=publisher_audience) publisher = pubsub.PublisherClient( batch_settings=types.BatchSettings(max_messages=500), credentials=credentials) class CircularReadFile: def __init__(self): self.open() def open(self): self.in_file = open("resources/in.txt", "r") def read(self): line = self.in_file.readline() if not line: self.open() return self.read()
def create_pub_client(creds): """ Build a publishing client for pubsub""" from google.cloud import pubsub return pubsub.PublisherClient( credentials=creds, batch_settings=types.BatchSettings(max_messages=50))