setting the topic when using the pyobj subfunctions in zeromq/python

巧了我就是萌 提交于 2019-12-11 02:26:52

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!