queueing-producer.py 607 Bytes
Newer Older
Nane Kratzke's avatar
Nane Kratzke committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import os, time, random, json, socket
import redis
from messaging import MQueue

host = os.environ.get("REDIS_SVC", "redis-svc")
port = int(os.environ.get("REDIS_PORT", "6379"))
channel = os.environ.get("CHANNEL", "xqueue")

me = socket.gethostname()
queue = MQueue(channel, redis.Redis(host=host, port=port, db=0))

i = 0
while True:
    i = i + 1 
    msg = f"Hi, this is message #{ i } from { queue.host }."
    n = random.randint(1, 10)
    time.sleep(n / 10)
    try:
        queue.publish({ 'text': msg })
    except Exception as ex:
        print(f"Could not publish message '{msg}' caused by {ex}")