我一直在使用ZeroMQ的请求/响应套接字,以在Web应用程序和用于卸载处理的从属应用程序之间交换消息。我注意到在某些情况下,并非所有发送的ZMQ消息实际上都被另一端接收到。甚至奇怪的是,即使使用IPC协议,这种情况也会发生,我认为这是相当可靠的。
在不产生任何错误的情况下,导致未发送已发送消息的原因是什么?
这是客户端代码的示例:
# ironic
class ReliableClient(object):
def _reconnect(self):
if self.socket:
self.socket.close()
self.socket = None
self.socket = self.context.socket(zmq.REQ)
self.socket.connect(self.server_url)
# Give the server 2 sec to respond
self.socket.RCVTIMEO = 2000
self.socket.SNDTIMEO = 2000
self.socket.LINGER = 3
def __init__(self, server_url=None, server_name=None):
self.socket = None
self.server_url = server_url
if server_name is None:
self.server_name = server_url
else:
self.server_name = server_name
self.lock = threading.Lock()
self.context = zmq.Context()
self._reconnect()
def msg(self, msg):
raw_out = dumps(msg)
# send
self.lock.acquire()
try:
self.socket.send(
raw_out,
copy=True
)
except zmq.ZMQError as ex:
log.exception(ex, '%s: failed to send', self.server_name)
self.lock.release()
raise CommunicationError('failed to send')
# receive
try:
raw_in = self.socket.recv()
except zmq.ZMQError as ex:
log.exception(ex, '%s: failed to receive', self.server_name)
raise CommunicationError('failed to receive')
finally:
self.lock.release()
msg_in = loads(raw_in)
return msg_in
最佳答案
我对IPC协议不熟悉,但是我已经广泛使用ZMQ / TCP。
如果正确使用TCP,即使是基于TCP的最简单的ZMQ REQ / REP模式也永远不会丢弃消息。如果您的网络连接或远程端点断开,它可能会无限期挂起,但它绝不会默默地失败。在某些情况下,某些套接字在设计上会丢弃消息。例如,某些邮件可能会在达到HWM时丢弃消息。
关于python - ZeroMQ的可靠性?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/18236368/