在 zeromq/python 中使用 pyobj 子函数时设置主题 [英] setting the topic when using the pyobj subfunctions in zeromq/python

查看:35
本文介绍了在 zeromq/python 中使用 pyobj 子函数时设置主题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在看 zeromq,我注意到有 socket.send_pyobj() 和 socket.recv_pyobj() 函数.

I have been looking at zeromq and i noticed there were socket.send_pyobj() and socket.recv_pyobj() functions.

我的问题是,如果 PUB/SUB 调用此主题,他们将如何设置主题.在我看到的使用常规发送的示例中,它总是两个字符串,中间有一个空格,第一个字符串将被视为一个主题.

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?

推荐答案

NVM.想出了怎么做.如果我希望将主题和 msg 视为一个并过滤对象,我应该使用 send_multipart.

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.

对于出版商.

   self.socket.send_multipart([b'status',pickle.dumps(msg2)])

对于接收者.

    socket.setsockopt(zmq.SUBSCRIBE, 'status')
    [topic,msg] = socket.recv_multipart()
    msg2 = pickle.loads(msg)
    print msg2['game']

我不知道为什么,但如果你使用他们的例子.http://zguide.zeromq.org/py:psenvsub 它表明我应该做 b'status' 在 socketopt 上,但如果我这样做,它没有过滤.

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.

这篇关于在 zeromq/python 中使用 pyobj 子函数时设置主题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆