<p>在我看来,它实际上像一只披萨上的虫子。下面是最终引发异常的connection.connect()代码:</p>
<pre><code>def connect(self):
"""Invoke if trying to reconnect to a RabbitMQ server. Constructing the
Connection object should connect on its own.
"""
self._set_connection_state(self.CONNECTION_INIT)
if self._adapter_connect():
return self._on_connected()
self.remaining_connection_attempts -= 1
LOGGER.warning('Could not connect, %i attempts left',
self.remaining_connection_attempts)
if self.remaining_connection_attempts:
LOGGER.info('Retrying in %i seconds', self.params.retry_delay)
self.add_timeout(self.params.retry_delay, self.connect)
else:
self.callbacks.process(0, self.ON_CONNECTION_ERROR, self, self)
self.remaining_connection_attempts = self.params.connection_attempts
self._set_connection_state(self.CONNECTION_CLOSED)
</code></pre>
<p>因此,<code>self._adapter_connect()</code>显然没有返回True,这表明连接失败。这里是<code>AsyncoreConnection._adapter_connect</code>代码:</p>
<pre><code>def _adapter_connect(self):
"""Connect to our RabbitMQ broker using AsyncoreDispatcher, then setting
Pika's suggested buffer size for socket reading and writing. We pass
the handle to self so that the AsyncoreDispatcher object can call back
into our various state methods.
"""
if super(AsyncoreConnection, self)._adapter_connect():
self.socket = PikaDispatcher(self.socket, None, self._handle_events)
self.ioloop = self.socket
self._on_connected()
</code></pre>
<p>它什么也不回!所以<code>connect</code>中的if语句永远不会是真的。如果我更改方法以反映所有其他适配器使用的模式:</p>
<pre><code>def _adapter_connect(self):
"""Connect to our RabbitMQ broker using AsyncoreDispatcher, then setting
Pika's suggested buffer size for socket reading and writing. We pass
the handle to self so that the AsyncoreDispatcher object can call back
into our various state methods.
"""
if super(AsyncoreConnection, self)._adapter_connect():
self.socket = PikaDispatcher(self.socket, None, self._handle_events)
self.ioloop = self.socket
return True
return False
</code></pre>
<p>它工作得很好。我一定会把那个臭虫归档的!</p>
<p><strong>编辑:</strong></p>
<p>此错误似乎已在最新版本(从<a href="https://github.com/pika/pika/blob/master/pika/adapters/asyncore_connection.py" rel="nofollow">github</a>)中修复:</p>
<pre><code> def _adapter_connect(self):
"""Connect to our RabbitMQ broker using AsyncoreDispatcher, then setting Pika's suggested buffer size for socket reading and writing. We pass the handle to self so that the AsyncoreDispatcher object can call back into our various state methods.
"""
error = super(AsyncoreConnection, self)._adapter_connect()
if not error:
self.socket = PikaDispatcher(self.socket, None,
self._handle_events)
self.ioloop = self.socket
self._on_connected()
return error
</code></pre>