hao123
hao123

Reputation: 441

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

I'm trying to use the IPC protocol with ZeroMQ in Python.

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)

It gives the following error:

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".

I don't understand why would this happen with the socket.bind() function, since in the documentation it says:

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.

which means that it is not necessary to supply an already created directory.

Upvotes: 3

Views: 2029

Answers (1)

tdelaney
tdelaney

Reputation: 77337

The URL scheme is ipc://. You need to add an absolute path /var/run/fast-service. So,

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

More generally the URL is ipc://<host>/<path>. You want local host, so that part is empty. A file system URL is similar, file:///home/foo/bar.txt references /home/foo/bar.txt on the local host.

Upvotes: 1

Related Questions