This appears to occur because of the "stop_ioloop_on_close=False" in the SelectConnection(). However, without that, the on_connection_closed() callback isn't reliably called and the timer based reconnect() callback is never called.
So here was my work around. First, call SelectConnection with "stop_ioloop_on_close=True". Then, change the main function to:
The sample should now run correctly, and reconnect automatically on disconnect. You could also remove the reconnection code from the consumer class as its no longer used. On an aside, I've also started checking the error code in the on_channel_close() callback. I'm trying to verify the existence of a queue using queue_declare() with passive=True and the only way to detect it doesn't exist is to see that the channel is closed with a 404. By catching it there, I don't go into a continuous connect/disconnect loop.
WARNING 2013-04-06 13:17:27,444 pika.adapters.base_connection _check_state_on_disconnect 157 : Socket closed when connection was open
INFO 2013-04-06 13:17:27,459 pika.channel _on_close 833 : <METHOD(['frame_type=1', 'channel_number=1', "method=<Channel.Close(['reply_code=0', 'reply_text=Not specified'])>"])>
WARNING 2013-04-06 13:17:27,467 pika.channel _on_close 836 : Received remote Channel.Close (0): Not specified
WARNING 2013-04-06 13:17:27,476 __main__ on_channel_closed 133 : Channel 1 was closed: (0) Not specified
WARNING 2013-04-06 13:17:27,486 pika.connection close 586 : Invoked while closing or closed
WARNING 2013-04-06 13:17:27,500 __main__ on_connection_closed 81 : Connection closed, reopening in 5 seconds: (0) Not specified
ERROR 2013-04-06 13:17:27,914 pika.adapters.base_connection _handle_events 297 : Error event 8, error(10038, 'An operation was attempted on something that is not a socket')
Traceback (most recent call last):
File "C:\RPi\Perforce\Field Comm Adapter RabbitMQ to Cassandra\test.py", line 347, in <module>
main()
File "C:\RPi\Perforce\Field Comm Adapter RabbitMQ to Cassandra\test.py", line 341, in main
example.run()
File "C:\RPi\Perforce\Field Comm Adapter RabbitMQ to Cassandra\test.py", line 317, in run
self._connection.ioloop.start()
File "C:\Python27\lib\site-packages\pika\adapters\select_connection.py", line 102, in start
self.poller.start()
File "C:\Python27\lib\site-packages\pika\adapters\select_connection.py", line 259, in start
self.poll()
File "C:\Python27\lib\site-packages\pika\adapters\select_connection.py", line 222, in poll
return self._handler(self.fileno, ERROR, error)
File "C:\Python27\lib\site-packages\pika\adapters\base_connection.py", line 298, in _handle_events
self._handle_error(error)
File "C:\Python27\lib\site-packages\pika\adapters\base_connection.py", line 271, in _handle_error
self.socket.fileno(), error_code)
AttributeError: 'NoneType' object has no attribute 'fileno'