forked from GoogleCloudPlatform/training-data-analyst
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsendData.py
36 lines (31 loc) · 1.05 KB
/
sendData.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import argparse
import concurrent.futures
import time
from google.cloud import pubsub_v1
def publish_messages(project, topic_name):
"""Publishes multiple messages to a Pub/Sub topic."""
# [START pubsub_quickstart_publisher]
# [START pubsub_publish]
publisher = pubsub_v1.PublisherClient()
topic_path = publisher.topic_path(project, topic_name)
f = open("sensorData.txt","r")
#for n in range(1, 10):
while True:
#data = u'Message number {}'.format(n)
data=f.readline()
if data == '':
break
else:
# Data must be a bytestring
data = data.encode('utf-8')
publisher.publish(topic_path, data=data)
print(data)
time.sleep(30)
print('Published messages.')
# [END pubsub_quickstart_publisher]
# [END pubsub_publish]
ap=argparse.ArgumentParser()
ap.add_argument("-p","--project", required=True)
ap.add_argument("-t","--topic_name", required=True)
args=vars(ap.parse_args())
publish_messages(args["project"], args["topic_name"])