【发布时间】:2017-08-17 04:08:56
【问题描述】:
我开始使用带有Publisher/Subscriber 引用的python 使用zeromq。但是,我没有找到任何有关如何处理队列中消息的文档。我想将最后收到的消息与队列的其余元素不同。
示例
publisher.py
import zmq
import random
import time
port = "5556"
topic = "1"
context = zmq.Context()
socket = context.socket(zmq.PUB)
socket.bind("tcp://*:%s" % port)
while True:
messagedata = random.randrange(1,215)
print "%s %d" % (topic, messagedata)
socket.send("%s %d" % (topic, messagedata))
time.sleep(.2)
订阅者.py
import zmq
port = "5556"
topic = "1"
context = zmq.Context()
socket = context.socket(zmq.SUB)
print "Connecting..."
socket.connect ("tcp://localhost:%s" % port)
socket.setsockopt(zmq.SUBSCRIBE,topic)
while True:
if isLastMessage(): # probably based on socket.recv()
analysis_function() # time consuming function
else:
simple_function() # something simple like print and save in memory
我只想知道如何创建subscriber.py 文件中描述的isLastMessage() 函数。如果直接在 zeromq 中有内容或解决方法。
【问题讨论】:
标签: python sockets zeromq publish-subscribe