How can I get Pika to try connecting to RabbitMQ again if this fails for the first time?

I am trying to get my program that uses Pika to constantly reconnect to RabbitMQ on failure. From what I saw in the Pika docs, there is a SimpleReconnectionStrategy class that can be used to escort, but it doesn't seem to work very well.

strategy        = pika.SimpleReconnectionStrategy()
parameters      = pika.ConnectionParameters(server)

self.connection = pika.AsyncoreConnection(parameters, True, strategy)
self.channel    = self.connection.channel()

The connection should wait_for_openand configure the reconnection strategy.

However, when I run this, I get the following errors:

error: uncaptured python exception, closing channel <pika.asyncore_adapter.RabbitDispatcher at 0xb6ba040c> (<class 'socket.error'>:[Errno 111] Connection refused [/usr/lib/python2.7/asyncore.py|read|79] [/usr/lib/python2.7/asyncore.py|handle_read_event|435] [/usr/lib/python2.7/asyncore.py|handle_connect_event|443])
error: uncaptured python exception, closing channel <pika.asyncore_adapter.RabbitDispatcher at 0xb6ba060c> (<class 'socket.error'>:[Errno 111] Connection refused [/usr/lib/python2.7/asyncore.py|read|79] [/usr/lib/python2.7/asyncore.py|handle_read_event|435] [/usr/lib/python2.7/asyncore.py|handle_connect_event|443])

These errors are constantly thrown while Pika is trying to connect. If I start the RabbitMQ server while my client is running, it will connect. I just don't like these mistakes ... Are they normal? Am I doing it wrong?

+5
1
import socket

...

while True:
    connectSucceeded = False
    try:
        self.channel    = self.connection.channel()
        connectSucceeded = True
    except socket.error:
        pass
    if connectSucceeded:
        break

. time.sleep() , , . ( , ), . .

+3

All Articles