[zeromq-dev] IPC and free ports

andrea crotti andrea.crotti.0 at gmail.com
Fri Oct 12 14:08:50 CEST 2012


2012/10/12 andrea crotti <andrea.crotti.0 at gmail.com>:
>
>
> 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://127.0.0.1:%d' % NEXT_PORT
>             NEXT_PORT += 1
>             ctx = zmq.Context()
>             temp_sock = ctx.socket(zmq.REP)
>             try:
>                 temp_sock.bind(channel)
>             except zmq.ZMQError:
>                 logger.debug("Channel %s is busy, taking next port" % channel)
>                 print("Channel %s is busy, taking next port" % channel)
>                 continue
>             else:
>                 temp_sock.close()
>                 break
>
>         return channel
>
>     if tcp:
>         channel = _get_next_open_port()
>     else:
>         # 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..

Ah no actually the file exists
srwxr-xr-x 1 andrea andrea     0 Oct 12 10:29 file

but path.isfile('file') returns false because it's a socket device, so
I'll have to find another way to check that it's there..

And just to make sure I understood, supposing a client connects and
starts to send some messages over ipc, it's not writing to that file
if there is nothing listening (with a bind) on it, right?



More information about the zeromq-dev mailing list