相关疑难解决方法(0)

RabbitMQ的Python教程代码无法运行

编辑:我在设备上安装了错误版本的pika软件包。我从点子更新后,效果很好。

我刚刚按照其教程开始学习RabbitMQ的用法(使用Python)。该send.py代码工作正常,但是当我尝试运行时receive.py,我看到此错误:

Traceback (most recent call last):
  File "receive.py", line 15, in <module>
    no_ack=True)
TypeError: basic_consume() got multiple values for keyword argument 'queue'
Run Code Online (Sandbox Code Playgroud)

这是里面的代码receive.py

#!/usr/bin/env python
import pika

connection = pika.BlockingConnection(pika.ConnectionParameters(host='localhost'))
channel = connection.channel()


channel.queue_declare(queue='hello')

def callback(ch, method, properties, body):
    print(" [x] Received %r" % body)

channel.basic_consume(callback,
                      queue='hello',
                      no_ack=True)

print(' [*] Waiting for messages. To exit press CTRL+C')
channel.start_consuming()
Run Code Online (Sandbox Code Playgroud)

知道我在做什么错吗?

python rabbitmq

6
推荐指数
3
解决办法
6212
查看次数

rabbitmq 中的多个消费者用于多个队列

我有 2 个队列,比如说 q1 和 q2,它们对应于具有绑定密钥 b1 和 b2 的 e1 和 e2 交换。我想并行运行消费者函数,比如 c1 和 c2,它们将分别监听 q1 和 q2。我尝试了以下方法:

def c1():
    connection = pika.BlockingConnection(pika.ConnectionParameters(host=constants.rmqHostIp))
    channel = connection.channel()
    channel.exchange_declare(exchange='e1', durable='true',
                         type='topic')
    result = channel.queue_declare(durable='false', queue='q1')
    queue_name = result.method.queue
    binding_key = "b1"
    channel.queue_bind(exchange='e1',
                       queue=queue_name,
                       routing_key=binding_key)
    channel.basic_consume(callback,queue=queue_name,no_ack=False)
    channel.start_consuming()

def c2():
    connection = pika.BlockingConnection(pika.ConnectionParameters(host=constants.rmqHostIp))
    channel = connection.channel()
    channel.exchange_declare(exchange='e2', durable='true',
                         type='topic')
    result = channel.queue_declare(durable='false', queue='q2')
    queue_name = result.method.queue
    binding_key = "b2"
    channel.queue_bind(exchange=e1,
                       queue=queue_name,
                       routing_key=binding_key)
    channel.basic_consume(callback,queue=queue_name,no_ack=False)
    channel.start_consuming()

if __name__ == '__main__':
    c1()
    c2()
Run Code Online (Sandbox Code Playgroud)

但是,它只监听 c1 …

python rabbitmq pika python-pika

2
推荐指数
1
解决办法
3674
查看次数

标签 统计

python ×2

rabbitmq ×2

pika ×1

python-pika ×1