当我在zmq指南中读到“持久订阅者和高水位标记”时,它说“HWM导致ØMQ丢弃它无法放入队列的消息”,但是当我运行该示例时没有消息丢失。按ctrl + c终止durasub.py,然后继续。
durasub.py
import zmq
import time
context = zmq.Context()
subscriber = context.socket(zmq.SUB)
subscriber.setsockopt(zmq.IDENTITY, "Hello")
subscriber.setsockopt(zmq.SUBSCRIBE, "")
subscriber.connect("tcp://localhost:5565")
sync = context.socket(zmq.PUSH)
sync.connect("tcp://localhost:5564")
sync.send("")
while True:
data = subscriber.recv()
print data
if data == "END":
break
durapub.py
import zmq
import time
context = zmq.Context()
sync = context.socket(zmq.PULL)
sync.bind("tcp://*:5564")
publisher = context.socket(zmq.PUB)
publisher.bind("tcp://*:5565")
publisher.setsockopt(zmq.HWM, 2)
sync_request = sync.recv()
for n in xrange(10):
msg = "Update %d" % n
publisher.send(msg)
time.sleep(1)
publisher.send("END")
答案 0 :(得分:16)
上述建议有效,但未正确解决此特定代码中的问题。
这里的真正问题是durapub.py
在publisher.setsockopt(zmq.HWM, 2)
调用publisher.bind
后呼叫setsockopt
。您应该在bind
或connect
之前致电{{1}}。
请参阅0MQ API documentation for setsockopt:
警告:除ZMQ_SUBSCRIBE,ZMQ_UNSUBSCRIBE和ZMQ_LINGER外,所有选项仅对后续套接字绑定/连接生效。