编辑:,我在我的设备上安装了错误版本的皮卡软件包。在我从pip上更新后,它就可以正常工作了。
我刚开始学习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'下面是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()知道我做错了什么吗?
发布于 2019-03-29 10:05:14
你可能不再需要它了,但我和你有同样的问题,这就是我想出来的。
对我来说,RabbitMQ文档一定是使用了不同版本的pika。我发现在Pika1.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‘,这一切都起作用了。希望能帮上忙!
发布于 2019-05-24 05:30:53
只要改变
channel.basic_consume(callback, queue='hello', no_ack=True)至
channel.basic_consume('hello', callback, auto_ack=True)发布于 2018-05-18 06:38:01
我不能重复你的错误,但我想尽量简明扼要。
一开始,我在我的计算机上设置了一个rabbitmq服务器作为码头集装箱,而不是为了污染我的系统:
$ docker run -d --hostname localhost --name some-rabbit rabbitmq:3然后,我使用IPAddress来查找我的rabbitmq容器实际上正在运行的:
$ docker inspect some-rabbit --format='{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}'
172.17.0.2接下来,我使用琵琶在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,您也可以在这里使用python2.7。
然后使用pipenv跳入环境:
~/example$ pipenv shell
Spawning environment shell (/bin/bash). Use 'exit' to leave.在这里,我按照send.py和receive.py的建议创建了两个文件pika示例文档,但是我将用上面提到的码头容器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
https://stackoverflow.com/questions/50404273
复制相似问题