ZeroMQ 在绑定到 ipc:// 协议地址的套接字上抛出 ZMQError (python)

ZeroMQ threw ZMQError on socket bind to an ipc:// protocol address (python)

我正在尝试在 Python 中将 IPC 协议与 ZeroMQ 一起使用。

import sys
import time

from random import randint

import zmq

def main(url=None):
    ctx = zmq.Context.instance()
    publisher = ctx.socket(zmq.PUB)
    if url:
        publisher.bind(url)
    else:
        publisher.bind('ipc://var/run/fast-service')
    # Ensure subscriber connection has time to complete
    time.sleep(1)

    # Send out all 1,000 topic messages
    for topic_nbr in range(1000):
        publisher.send_multipart([
            b"%03d" % topic_nbr,
            b"Save Roger",
        ])

if __name__ == '__main__':
    main(sys.argv[1] if len(sys.argv) > 1 else None)

它给出了以下错误:

Traceback (most recent call last):
  File "pathopub.py", line 43, in <module>
    main(sys.argv[1] if len(sys.argv) > 1 else None)
  File "pathopub.py", line 19, in main
    publisher.bind("ipc://var/run/fast-service")
  File "zmq/backend/cython/socket.pyx", line 547, in zmq.backend.cython.socket.Socket.bind
zmq.error.ZMQError: No such file or directory for ipc path "var/run/fast-service".

我不明白为什么 socket.bind() 函数会发生这种情况,因为在 documentation 中它说:

When binding a socket to a local address using zmq_bind() with the ipc transport, the endpoint shall be interpreted as an arbitrary string identifying the pathname to create.

这意味着没有必要提供一个已经创建的目录。

URL 方案是 ipc://。您需要添加一个绝对路径/var/run/fast-service。所以,

publisher.bind('ipc:///var/run/fast-service')

更一般地说,URL 是 ipc://<host>/<path>。你想要本地主机,所以那部分是空的。文件系统 URL 类似,file:///home/foo/bar.txt 引用本地主机上的 /home/foo/bar.txt