BTW, I don't know if this is helpful to you (haven't followed this thread very close) but pyzmq has a bind_to_random_port socket method which does the same as your loop code below.
https://github.com/zeromq/pyzmq/blob/master/zmq/core/pysocket.py#L114 -Michel On Fri, Oct 12, 2012 at 2:31 AM, andrea crotti <[email protected]> wrote: > 2012/10/11 Ian Barber <[email protected]>: >> On Thu, Oct 11, 2012 at 5:14 PM, andrea crotti >> <[email protected]> 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 >> [email protected] >> 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://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.. > _______________________________________________ > zeromq-dev mailing list > [email protected] > http://lists.zeromq.org/mailman/listinfo/zeromq-dev _______________________________________________ zeromq-dev mailing list [email protected] http://lists.zeromq.org/mailman/listinfo/zeromq-dev
