I have some simple PUB/SUB code using the ZeroMQ library. Essentially just waiting infinitely for messages to be returned.
However, this seems to be busy waiting as while waiting to receive a message, pythons CPU usage raises dramatically.
Is there a better way to do this? I imagined this would be like on a socket when you call receive, it isn't constantly executing instructions and wasting CPU.
import sys
import zmq
port = "5556"
if len(sys.argv) > 1:
port = sys.argv[1]
int(port)
# Socket to talk to server
context = zmq.Context()
socket = context.socket(zmq.SUB)
print "Collecting updates from weather server..."
socket.connect ("tcp://localhost:%s" % port)
# Subscribe to zipcode, default is NYC, 10001
topicfilter = "10001"
socket.setsockopt(zmq.SUBSCRIBE, topicfilter)
# Process 5 updates
total_value = 0
for update_nbr in range (5):
string = socket.recv()
topic, messagedata = string.split()
total_value += int(messagedata)
print ('{} {}'.format(topic, message)