RabbitMQ的Python教程代码无法运行

vfh0ocws  于 4个月前  发布在  RabbitMQ
关注(0)|答案(6)|浏览(60)

**编辑:**我在设备上安装了错误版本的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()


知道我哪里做错了吗

vhmi4jdf

vhmi4jdf1#

你可能不再需要它了,但我和你有同样的问题,这是我发现的。
对我来说,原来RabbitMQ文档一定是使用了不同版本的pika。我发现在pika 1.0.0中,basic_consume函数有不同的参数顺序。这是它在我的机器上的样子:

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

字符串
一旦我改变了参数传递的顺序,或者添加了关键字'on_message_callback=callback',一切都正常了。我希望这对你有帮助!

n1bvdmb6

n1bvdmb62#

只是改变

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

字符串

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

rseugnpd

rseugnpd3#

我不能重现你的错误,但我想尽可能简洁,当试图。
首先,我在我的电脑上设置了一个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.


在那里,我创建了两个文件send.pyreceive.py,正如example documentation of pika所建议的那样,但我将用上面的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,f3rdy

bvk5enib

bvk5enib4#

我在Ubuntu 18.04上使用库存python-pika包版本0.11.0-1遇到了同样的问题,在我删除库存版本并通过pip安装了更新版本的pika(1.0.1)后,这个问题消失了。

4szc88ey

4szc88ey5#

只是改变

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

字符串

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


因为我在lib中找到了代码。

aurhwmvo

aurhwmvo6#

这段代码可能会有帮助:

channel.basic_consume(on_message_callback=callback, queue='hello')

字符串

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

相关问题