编辑:我在设备上安装了错误版本的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)
知道我在做什么错吗?
Pio*_*eba 13
您可能不再需要它了,但是我遇到了与您完全相同的问题,这就是我所发现的。
对我来说,事实证明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):
Run Code Online (Sandbox Code Playgroud)
一旦我更改了传递的参数顺序,或添加了关键字“ on_message_callback = callback”,它们全部起作用。希望对您有所帮助!
小智 7
只是改变
channel.basic_consume(callback, queue='hello', no_ack=True)
Run Code Online (Sandbox Code Playgroud)
至
channel.basic_consume('hello', callback, auto_ack=True)
Run Code Online (Sandbox Code Playgroud)
我无法重现您的错误,但在尝试时我希望尽可能简洁。
\n\n首先,我在我的计算机上设置了一个rabbitmq服务器作为docker容器,以免污染我的系统:
\n\n$ docker run -d --hostname localhost --name some-rabbit rabbitmq:3\nRun Code Online (Sandbox Code Playgroud)\n\n然后我使用检查来查找我的rabbitmq容器实际运行的IP地址:
\n\n$ docker inspect some-rabbit --format=\'{{range .NetworkSettings.Networks}}{{.IPAddress}}{{end}}\'\n172.17.0.2\nRun Code Online (Sandbox Code Playgroud)\n\n接下来我使用pipelinev在 python3 中创建一个虚拟环境,其中至少包含 pika 和依赖项,以遵循示例:
\n\n$ mkdir example && cd example && pipenv --three install pika\nCreating a virtualenv for this project\xe2\x80\xa6\nUsing /usr/bin/python3 (3.6.5) to create virtualenv\xe2\x80\xa6\nRun Code Online (Sandbox Code Playgroud)\n\n注意,如果你pipenv --two在安装 pika 时说,你也可以在这里使用 python 2.7。
然后使用pipenv shell跳转到环境:
\n\n~/example$ pipenv shell\nSpawning environment shell (/bin/bash). Use \'exit\' to leave.\nRun Code Online (Sandbox Code Playgroud)\n\n我在那里创建了两个文件send.py,并按照pika 示例文档的receive.py建议,但我将用上面的 docker 容器 IP 替换:localhost
$ cat send.py \n#!/usr/bin/env python \nimport pika\n\nconnection = pika.BlockingConnection(pika.ConnectionParameters(host=\'172.17.0.2\'))\nchannel = connection.channel()\n\n\nchannel.queue_declare(queue=\'hello\')\n\nchannel.basic_publish(exchange=\'\',\n routing_key=\'hello\',\n body=\'Hello World!\')\nprint(" [x] Sent \'Hello World!\'")\nconnection.close()\nRun Code Online (Sandbox Code Playgroud)\n\n和receive.py:
$ cat receive.py\n#!/usr/bin/env python\nimport pika\n\nconnection = pika.BlockingConnection(pika.ConnectionParameters(host=\'172.17.0.2\'))\nchannel = connection.channel()\n\n\nchannel.queue_declare(queue=\'hello\')\n\ndef callback(ch, method, properties, body):\n print(" [x] Received %r" % body)\n\nchannel.basic_consume(callback,\n queue=\'hello\',\n no_ack=True)\n\nprint(\' [*] Waiting for messages. To exit press CTRL+C\')\nchannel.start_consuming()\nRun Code Online (Sandbox Code Playgroud)\n\n在一个终端中运行 receive.py 并在另一个终端中运行 send.py 可以按预期工作:
\n\n $ python receive.py \n [*] Waiting for messages. To exit press CTRL+C\n\n $ python send.py\n [x] Sent \'Hello World!\'\n\n $ python receive.py \n [*] Waiting for messages. To exit press CTRL+C\n [x] Received b\'Hello World!\nRun Code Online (Sandbox Code Playgroud)\n\nHTH,\nf3rdy
\n| 归档时间: |
|
| 查看次数: |
6212 次 |
| 最近记录: |