basic_consume() got multiple values for keyword argument 'queue'

在用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")

再执行脚本即能正常运行

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

banche168

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值