我一直在使用 ZeroMQ 的请求/响应套接字来在 Web 应用程序和用于卸载处理的从属应用程序之间交换消息。我注意到在少数情况下,并非所有发送的 ZMQ 消息都被对方实际接收。甚至奇怪的是,即使使用IPC协议也会发生这种情况,我认为 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