[zeromq-dev] IPC and free ports

andrea crotti andrea.crotti.0 at gmail.com
Fri Oct 12 11:31:51 CEST 2012

2012/10/11 Ian Barber <ian.barber at gmail.com>:
> On Thu, Oct 11, 2012 at 5:14 PM, andrea crotti
> <andrea.crotti.0 at gmail.com> wrote:
>> So suppose I want to make sure that I'm not binding twice, what should I
>> do? Check if the file already exists or?
> Yep
>> try:
>>      sock.bind(addr)
>> except zmq.Error:
>>     # loop over until find a free channel
> That works, you can also bind to port * and check the
> ZMQ_LAST_ENDPOINT sockopt to get what port was chosen by the OS.
> Ian
> _______________________________________________
> zeromq-dev mailing list
> zeromq-dev at lists.zeromq.org
> http://lists.zeromq.org/mailman/listinfo/zeromq-dev

Ok thanks I wrote this which is a bit ugly but seems to work for now..
I just want to be ableto switch from IPC to TCP easily, to see what
works best in my case..

def get_channel(name, tcp=False):
    """Return a channel, by default using IPC or tcp if requested.
    def _get_next_open_port():
        global NEXT_PORT
        while True:
            channel = 'tcp://' % NEXT_PORT
            NEXT_PORT += 1
            ctx = zmq.Context()
            temp_sock = ctx.socket(zmq.REP)
            except zmq.ZMQError:
                logger.debug("Channel %s is busy, taking next port" % channel)
                print("Channel %s is busy, taking next port" % channel)

        return channel

    if tcp:
        channel = _get_next_open_port()
        # TODO: check that the file is there
        channel = 'ipc://%s' % name

    logger.debug("Generating channel %s" % channel)
    return channel

But why double binding on IPC doesn't fail, when should that be a good idea?

And what file I should actually check, because I can't find it anywhere:

In [39]: sock.bind('ipc://file')

In [40]: cat file
cat: file: No such device or address

If I understood that should create a file in the local directory..

More information about the zeromq-dev mailing list