Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python tutorial code from RabbitMQ failing to run

Edit: I had the wrong version of the pika package installed on my device. It works fine after I updated from pip.

I just started learning the usage of RabbitMQ (using Python) by following their tutorial. The send.py code works works fine but when I try to run receive.py, I see this error:

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'

Here's the code inside 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()

Any idea what I am doing wrong?

like image 361
sudhanva Avatar asked May 18 '18 05:05

sudhanva


2 Answers

You probably don't need it anymore, but I had exactly the same problem as You and this is what I figured out.

For me it turned out that RabbitMQ documentation must've been using a different version of pika. I found out that in pika 1.0.0 basic_consume function has different order of arguments. This is how it looks on my machine:

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

Once I changed the order of arguments passed, or added keyword 'on_message_callback=callback' it all worked. I hope it helps!

like image 191
Piotr Wieleba Avatar answered Oct 08 '22 08:10

Piotr Wieleba


just change

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

to

channel.basic_consume('hello', callback, auto_ack=True)
like image 33
maschzh Avatar answered Oct 08 '22 07:10

maschzh