Skip to content

Instantly share code, notes, and snippets.

@ymotongpoo
Created March 27, 2012 15:12
Show Gist options
  • Save ymotongpoo/2216771 to your computer and use it in GitHub Desktop.
Save ymotongpoo/2216771 to your computer and use it in GitHub Desktop.
ZeroMQ PUB/SUB sample implementation
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))
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)
@voluntas
Copy link

tcp だと同時に 50 個くらいにしか配れないんだよね。icp 使うとイイヨ。icp 使えるように zmq 側をビルドする必要あるけど ... 。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment