我有这个代码:
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
在我运行之后consumer.consume()
- 我的a
变量仍然是一个空列表,但在 rabbitmq 队列中现在是空的,这意味着它们被消耗掉了。
进一步尝试将更多消息放入队列是毫无意义的,因为队列保持为空,就好像我没有连接或消息被直接消耗掉一样。
我做错了什么?