在用rabbitmq调试生产者消费者demo时,报错如题目所示
详细报错信息如下:
Traceback (most recent call last):
File "hello_consumer.py", line 19, in <module>
channel.basic_consume(msg_consumer, queue="hello-queue", consumer_tag="hello-consumer")
TypeError: basic_consume() got multiple values for keyword argument 'queue'
源码如下:
import pika
credentials = pika.PlainCredentials("guest", "guest")
conn_params = pika.ConnectionParameters("localhost", credentials=credentials)
conn_broker = pika.BlockingConnection(conn_params)
channel = conn_broker.channel()
channel.exchange_declare(exchange="hello-exchange", exchange_type="direct", passive=False, durable=True, auto_delete=False)
channel.queue_declare(queue="hello-queue")
channel.queue_bind(queue="hello-queue", exchange="hello-exchange", routing_key="hola")
def msg_consumer(channel, method, header, body):
channel.basic_ack(delivery_tag=method.delivery_tag)
if body == "quit":
channel.basic_cancel(consumer_tag="hello-consumer")
channel.stop_consuming()
else:
print body
return
channel.basic_consume(msg_consumer, queue="hello-queue", consumer_tag="hello-consumer")
channel.start_consuming()
查看api发现参数位置发生了变化,修改回调函数和queue的位置即可
channel.basic_consume(queue="hello-queue", msg_consumer, consumer_tag="hello-consumer")
调整后运行仍然报错,
File "hello_consumer.py", line 19
channel.basic_consume(queue="hello-queue", msg_consumer, consumer_tag="hello-consumer")
SyntaxError: non-keyword arg after keyword arg
再次修改参数,第一个参数不能是键值对,最后如下所示:
channel.basic_consume("hello-queue", msg_consumer, consumer_tag="hello-consumer")
再执行脚本即能正常运行