问题
I have been looking at zeromq and i noticed there were socket.send_pyobj() and socket.recv_pyobj() functions.
My question is how would one set the topic for PUB/SUB if they called this. In the examples i have seen that have used the regular send it was always two strings with a space in between and first string would be considered a topic.
topic = 'test'
msg = 'hello'
socket.send("%s %s" % (topic,msg))
Is there a way? Or maybe should i use different ports as a way to make different topics?
回答1:
NVM. figured out how to do it. I should use send_multipart if i want the topic and msg viewed as one and also filter the object.
For the publisher.
self.socket.send_multipart([b'status',pickle.dumps(msg2)])
For the reciver.
socket.setsockopt(zmq.SUBSCRIBE, 'status')
[topic,msg] = socket.recv_multipart()
msg2 = pickle.loads(msg)
print msg2['game']
I don't know why but if you use their example. http://zguide.zeromq.org/py:psenvsub it shows i should do b'status' on the socketopt but it didnt filter if i did it that wya.
来源:https://stackoverflow.com/questions/24048319/setting-the-topic-when-using-the-pyobj-subfunctions-in-zeromq-python