如何使用 PyZMQ 在 ZeroMQ PUB 套接字上设置 INVERT_MATCHING?

How do I set INVERT_MATCHING on a ZeroMQ PUB socket with PyZMQ?

我正在使用 ZeroMQ 4.1.6 支持的 PyZMQ 16.0.4,并试图更好地了解套接字选项 INVERT_MATCHING.

我已经在 repl 中尝试了以下内容:

>>> import zmq
>>> context = zmq.Context.instance()
>>> pubsock = context.socket(zmq.PUB)
>>> pubsock.INVERT_MATCHING = 1

最后一行报错

AttributeError: Socket has no such option: INVERT_MATCHING.

我只是看到 PyZMQ 中的错误吗?

或者我需要做一些不同的事情来设置这个选项吗?

开始吧:使用.setsockopt()

print( "ZeroMQ version ought be 4.2+ ... Hope it is... " )
print( zmq.version() )
print( pubsock.setsockopt.__doc__ )
pass;  pubsock.setsockopt( zmq.INVERT_MATCHING, 1 )

加上最好的re-read文档:

Reverses the filtering behavior of PUB-SUB sockets, when set to 1.

On PUB and XPUB sockets, this causes messages to be sent to all connected sockets except those subscribed to a prefix that matches the message. On SUB sockets, this causes only incoming messages that do not match any of the socket's subscriptions to be received by the user.

Whenever ZMQ_INVERT_MATCHING is set to 1 on a PUB socket, all SUB sockets connecting to it must also have the option set to 1. Failure to do so will have the SUB sockets reject everything the PUB socket sends them.

XSUB sockets do not need to do this because they do not filter incoming messages.

ZeroMQ PUB-自 version 4.2 以来套接字仅支持反向匹配。您需要更新您的 ZeroMQ。

请注意,PyZMQ 捆绑了预编译的 ZeroMQ 4.1.6。因此,根据您安装 PyZMQ 的方式,您实际上可能没有使用系统的 ZeroMQ。

尝试以下方法重新安装 PyZMQ 并强制重新编译 ZeroMQ 后端。

$ pip install pyzmq --ignore-installed --no-use-wheel