为RabbitMQ重新连接puka python库

时间:2013-11-14 16:10:45

标签: python rabbitmq

我使用puka python库连接几个连接到RabbitMQ的客户端,一段时间不使用连接时会超时。目前的puka API仅提供连接,不支持重新连接。我试过使用普通的连接函数,但是因为我覆盖了_client和promise变量而收到错误。如何在不破坏其他连接的情况下将进程重新连接到rabbitMQ?谢谢!

当前代码如下所示,并由多个进程使用:

_client = puka.Client(amqp_url=URL)
promise = _client.connect()
_client.wait(promise)

promise = _client.exchange_declare(exchange='notifications')
_client.wait(promise)

promise = _client.queue_declare(queue=_queue, durable=True)
_client.wait(promise)

promise = _client.queue_bind(exchange='notifications', queue=_queue, routing_key=_queue)
_client.wait(promise)

consume_promise = _client.basic_consume(queue=_queue)

while not CONDITION:
    try:
        result = _client.wait(consume_promise, timeout=1)
        if not result:
            continue
    except puka.ConnectionBroken as e:
        # An error is raised here when it disconnects from Rabbit
        continue

    _client.basic_ack(result)

0 个答案:

没有答案