2014-09-04 83 views
0

我需要循環回調隊列中的所有消息,然後關閉回調。我需要它在隊列清空後停止使用。 因此,我正在將消息從一個隊列寫入另一個隊列。使用pika將所有消息從一個隊列重新排列到另一個隊列

creds = pika.PlainCredentials(app.config['mq.user'], app.config['mq.pass']) connection = pika.BlockingConnection(pika.ConnectionParameters( host=app.config['mq.host'], credentials=creds)) connection2 = pika.BlockingConnection(pika.ConnectionParameters( host=app.config['mq.host'], credentials=creds))

channel = connection.channel() 
    channel2 = connection2.channel() 

Def requeue_callback(ch, method, properties, body): 
try: 
    msg = json.loads(body) 
    ch2.basic_publish(exchange='', 
         routing_key=base_queue+'.request', 
         body = msg['orig_msg']) 
finally: 
    ch.basic_ack(delivery_tag = method.delivery_tag) 

channel.basic_consume(requeue_callback, 
        queue=base_queue+'.error') 

channel.start_consuming() 

*另外,我能找到一個隊列的消息的數量,然後消費該具體數目。在那種情況下,我將如何重新排列特定的號碼。

回答

0

在您的回調函數中,您可以用passive=True聲明隊列,然後使用結果來獲得消息計數。

例如:

>>> ch = rabbit.connection.channel() 
>>> method = ch.queue_declare('sandbox', passive=True) 
>>> method.method.message_count 
23 
>>> 

然後檢查是否此數字爲0

相關問題