Following the example in zmq documentation, here is my take with minor changes.
import zmq
import random
import time
port = "5556"
context = zmq.Context()
socket = context.socket(zmq.PUB)
socket.bind("tcp://127.0.0.1:%s" % port)
while True:
topic = random.randrange(9999,10005)
messagedata = random.randrange(1,215) - 80
print "%d %d" % (topic, messagedata)
socket.send("%d %d" % (topic, messagedata))
time.sleep(1)
import sys
import zmq
port = "5556"
context = zmq.Context()
socket = context.socket(zmq.SUB)
print "Collecting updates from weather server..."
socket.connect ("tcp://127.0.0.1:%s" % port)
while True:
print socket.recv()
I then execute them from terminal, and while publisher is publishing (i.e. printing outs the lines), subscriber never gets out of socket.recv()
adding
socket.setsockopt(zmq.SUBSCRIBE, "")
to subscriber, did the trick!
here is the updated subscriber.py
import sys
import zmq
port = "5556"
context = zmq.Context()
socket = context.socket(zmq.SUB)
socket.setsockopt(zmq.SUBSCRIBE, "")
print "Collecting updates from weather server..."
socket.connect ("tcp://127.0.0.1:%s" % port)
while True:
print socket.recv()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With