我试图在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
。所以更一般地说,URL是
ipc://<host>/<path>
。您需要本地主机,因此该部分为空。文件系统URL类似于本地主机上的file:///home/foo/bar.txt
引用/home/foo/bar.txt
相关问题 更多 >
编程相关推荐