Python 中的 ZeroMQ 和多个订阅过滤器 [英] ZeroMQ and multiple subscribe filters in Python

查看:22
本文介绍了Python 中的 ZeroMQ 和多个订阅过滤器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 Python 中使用 ZeroMQ 订阅多个过滤器,使用一个套接字.

I'd like to subscribe to multiple filters with ZeroMQ in Python, using one socket.

sock.setsockopt(zmq.SUBSCRIBE, 'first.filter')
sock.setsockopt(zmq.SUBSCRIBE, 'second.filter')

但这行不通.只考虑第一个.但是,我在 zeromq 站点 上阅读了此内容:

But this doesn't work. Only the first one is taken in account. However, I read this on zeromq site:

多个过滤器可以附加到单个 ZMQ_SUB 套接字,在这种情况下,如果消息匹配至少一个过滤器,则应接受该消息.

Multiple filters may be attached to a single ZMQ_SUB socket, in which case a message shall be accepted if it matches at least one filter.

我使用的是 zmq 2.2.0.1.所以,我想知道如何做到这一点.有什么想法吗?

I'm using zmq 2.2.0.1. So, I wonder how to do that. Any ideas?

推荐答案

这有效:

import time
import zmq

ctx = zmq.Context()
pub = ctx.socket(zmq.PUB)
sub = ctx.socket(zmq.SUB)

url = "tcp://127.0.0.1:5555"
pub.bind(url)
sub.connect(url)

# subscribe to 'a' and 'b'
sub.setsockopt(zmq.SUBSCRIBE, b'a')
sub.setsockopt(zmq.SUBSCRIBE, b'b')

time.sleep(1)

for word in [ 'alpha', 'beta', 'gamma', 'apple', 'carrot', 'bagel']:
    pub.send(word)

time.sleep(1)

for i in range(4):
    print sub.recv(zmq.NOBLOCK)

给出输出:

alpha
beta
apple
bagel

所以两种订阅都有效.你的确切代码是什么?因为也许这是另一个问题.

So both subscriptions do work. What's your exact code? Because maybe it's another issue.

这篇关于Python 中的 ZeroMQ 和多个订阅过滤器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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