0

I have this code:

from kombu.transport import pyamqp
from kombu import Connection, Exchange, Queue, Producer, Consumer

transport = pyamqp
vhost = '/'
host = '127.0.0.1'
port = '5672'
usr = 'guest'
pwd = 'guest'

conn = Connection(virtual_host=vhost, hostname=host, port=port, userid=usr, password=pwd)
chan = conn.channel()

exchange = Exchange(name='ami_exch', type='topic', channel=chan, durable=True, auto_delete=True, delivery_mode='persistent')
exchange.declare(nowait=False)

q_ctl = Queue(name='ami.ctl', channel=chan, exchange=exchange, routing_key='ami.ctl.#', durable=True, auto_delete=True, exclusive=False)
q_ctl.declare(nowait=False)
q_str = Queue(name='ami.str', channel=chan, exchange=exchange, routing_key='ami.str.*', durable=True, auto_delete=True, exclusive=False)
q_str.declare(nowait=False)

msg = exchange.Message("Test MQ message.")
exchange.publish(message=msg, routing_key='ami.ctl')
exchange.publish(message=msg, routing_key='ami.str.send')

a = []
def tst(body, message):
    a.append(body)

consumer = Consumer(channel=chan, queues=[q_ctl,q_str], auto_declare=False, no_ack=True, callbacks=tst)
consumer.consume()
print a

After I run consumer.consume() - nothing happens in terms that my a variable remains an empty list, but in the rabbitmq queues now are empty, meaning that they were consumed.

Further attempts to put more messages into the queues are pointless as queues remain empty as if I was not connected or messages are consumed straightaway.

What am I doing incorrectly?

NarūnasK
  • 4,564
  • 8
  • 50
  • 76
  • How are you verifying that `a` remains empty? In any case you probably want to append `body` or `message`, rather than `tst`. – Daniel Roseman Jan 09 '16 at 22:54
  • Sorry, it was a typo, fixed now. I run this code in the `ipython` interactive console and print out `a` afterwards. – NarūnasK Jan 09 '16 at 22:56
  • Well this code is incomplete because you haven't even called `consumer.consume()`, so it is never actually going to start reading from the queue. – Daniel Roseman Jan 09 '16 at 22:58
  • I've mentioned that I run `consumer.consume()`as soon as I've checked in the rabbitmq, that I have declared exchange, queues and passed messages across. Added `consumer.consume()` to my code snippet for the clarity. – NarūnasK Jan 09 '16 at 23:03

0 Answers0