-
Notifications
You must be signed in to change notification settings - Fork 0
/
publish_messages.py
32 lines (24 loc) · 1.08 KB
/
publish_messages.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
import json
import os
import time
from google.cloud import pubsub_v1
project_id = os.environ["GOOGLE_CLOUD_PROJECT"]
topic_id = os.environ["topic"].split("/")[-1]
publisher = pubsub_v1.PublisherClient()
topic_path = publisher.topic_path(project_id, topic_id)
def generate_message(user, products):
return json.dumps({"user": user, "products": products,}).encode("utf-8")
messages = [
generate_message("user_1", [{"id": "prod_1", "quantity": 1,}]),
generate_message("user_1", [{"id": "prod_1", "quantity": 1,}]),
generate_message("user_1", [{"id": "prod_1", "quantity": 1,}]),
generate_message("user_2", [{"id": "prod_1", "quantity": 2,}]),
generate_message("user_2", [{"id": "prod_1", "quantity": 2,}]),
generate_message("user_2", [{"id": "prod_1", "quantity": 2,}]),
generate_message("user_3", [{"id": "prod_1", "quantity": 3,}]),
generate_message("user_3", [{"id": "prod_1", "quantity": 3,}]),
generate_message("user_3", [{"id": "prod_1", "quantity": 3,}]),
]
for message in messages:
publisher.publish(topic_path, data=message)
# time.sleep(1)