RabbitMQ中的Python教程代码无法运行

2024-05-28 23:25:36 发布

您现在位置:Python中文网/ 问答频道 /正文

编辑:我的设备上安装了错误版本的pika软件包。在我从pip更新后它工作得很好。

我刚刚开始学习RabbitMQ(使用Python)的用法,方法是遵循它们的tutorialsend.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'

下面是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()

知道我做错了什么吗?


Tags: no代码pytruehelloforbasicqueue
3条回答

你可能不再需要它了,但我和你有完全一样的问题,这就是我发现的。

对我来说,RabbitMQ文档肯定使用了不同版本的pika。我发现在pika 1.0.0中,基本的消费函数有不同的参数顺序。这就是它在我的机器上的样子:

    def basic_consume(self,
                  queue,
                  on_message_callback,
                  auto_ack=False,
                  exclusive=False,
                  consumer_tag=None,
                  arguments=None):

一旦我更改了传递参数的顺序,或者添加了关键字“on_message_callback=callback”,一切都正常。希望有帮助!

只是改变

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

channel.basic_consume('hello', callback, auto_ack=True)

我不能重复你的错误,但我想尽量简洁,当试图。

首先,我在我的计算机上设置了rabbitmq服务器as docker container,以避免污染我的系统:

$ docker run -d --hostname localhost --name some-rabbit rabbitmq:3

然后我使用inspect查找我的rabbitmq容器实际运行的IPAddress:

$ docker inspect some-rabbit --format='{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}'
172.17.0.2

接下来,我使用pipenv在python3中创建一个虚拟环境,其中至少包含pika和依赖项,如下所示:

$ mkdir example && cd example && pipenv --three install pika
Creating a virtualenv for this project…
Using /usr/bin/python3 (3.6.5) to create virtualenv…

注意,如果在安装pika时说pipenv --two,也可以在这里使用python 2.7。

然后使用pipenv shell跳入环境:

~/example$ pipenv shell
Spawning environment shell (/bin/bash). Use 'exit' to leave.

在这里,我创建了example documentation of pika建议的send.pyreceive.py两个文件,但是我将用上面的docker容器IP替换localhost

$ cat send.py 
#!/usr/bin/env python 
import pika

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


channel.queue_declare(queue='hello')

channel.basic_publish(exchange='',
                      routing_key='hello',
                      body='Hello World!')
print(" [x] Sent 'Hello World!'")
connection.close()

以及receive.py

$ cat receive.py
#!/usr/bin/env python
import pika

connection = pika.BlockingConnection(pika.ConnectionParameters(host='172.17.0.2'))
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()

在一个终端上运行receive.py,在另一个终端上运行send.py,可以正常工作:

 $ python receive.py 
 [*] Waiting for messages. To exit press CTRL+C

 $ python send.py
 [x] Sent 'Hello World!'

 $ python receive.py 
 [*] Waiting for messages. To exit press CTRL+C
 [x] Received b'Hello World!

HTH公司, 三日

相关问题 更多 >

    热门问题