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

2024-04-26 23:07:05 发布

您现在位置: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.

这意味着不需要提供已经创建的目录


Tags: inimportnoneurliftimebindmain
1条回答
网友
1楼 · 发布于 2024-04-26 23:07:05

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

相关问题 更多 >