Created
March 27, 2012 15:12
-
-
Save ymotongpoo/2216771 to your computer and use it in GitHub Desktop.
ZeroMQ PUB/SUB sample implementation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import zmq | |
import sys | |
context = zmq.Context() | |
subscriber = context.socket(zmq.SUB) | |
subscriber.connect("tcp://localhost:5556") | |
# receive only message with zipcode being 10001 | |
zipfilter = sys.argv if len(sys.argv) > 1 else "10001 " | |
subscriber.setsockopt(zmq.SUBSCRIBE, zipfilter) | |
update_samples = 10 | |
for updates in range(update_samples): | |
message = subscriber.recv() | |
zipcode, temprature, relhumidity, message_id = message.split() | |
print ("zip:%s, temp:%s, relh:%s, id:%s" % | |
(zipcode, temprature, relhumidity, message_id)) | |
total_temp = float(temprature) | |
print ("average temprature for zipcode '%s' was '%f'" % | |
(zipfilter, total_temp / update_samples)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import zmq | |
from random import randrange | |
context = zmq.Context() | |
publisher = context.socket(zmq.PUB) | |
publisher.bind("tcp://*:5556") | |
message_id = 0 | |
while True: | |
zipcode = randrange(10001, 10010) | |
temprature = randrange(0, 215) - 80 | |
relhumidity = randrange(0, 50) + 10 | |
update = "%05d %d %d %d" % (zipcode, temprature, relhumidity, message_id) | |
message_id += 1 | |
print update | |
publisher.send(update) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
tcp だと同時に 50 個くらいにしか配れないんだよね。icp 使うとイイヨ。icp 使えるように zmq 側をビルドする必要あるけど ... 。