def _bind_with_timeout(bind_function, args, n_tries=3, retry_interval_s=0.5):
"""Attempt to bind a socket a number of times with a short interval in between
Especially on Linux, crashing out of a networkzero process can leave the sockets
lingering and unable to re-bind on startup. We give it a few goes here to see if
we can bind within a couple of seconds.
"""
n_tries_left = n_tries
while n_tries_left > 0:
try:
return bind_function(*args)
except zmq.error.ZMQError as exc:
_logger.warn("%s; %d tries remaining", exc, n_tries_left)
n_tries_left -= 1
except OSError as exc:
if exc.errno == errno.EADDRINUSE:
_logger.warn("%s; %d tries remaining", exc, n_tries_left)
n_tries_left -= 1
else:
raise
else:
raise core.SocketAlreadyExistsError("Failed to bind after %s tries" % n_tries)
评论列表
文章目录