RabbitMQ and Pika – Error Messages

So I was trying to do some work with RabbitMQ using Python and Pika. Namely, I want to write message queues for use in some of my applications that let me do stuff asynchronously, so that the Python program is not blocking for any significant amount of time.

For that I installed rabbitmq-server and the package python3-pika on an Ubuntu 19.04 box.

Then, as I always do, I test the installation with some sample code that I suppose is working. The most likely candidate for this is the “Hello World” example from the RabbitMQ website. It should work right out of the box, right?

First, send.py:

#!/usr/bin/env python
import pika

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

Then 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(
    queue='hello', on_message_callback=callback, auto_ack=True)

print(' [*] Waiting for messages. To exit press CTRL+C')
channel.start_consuming()

The send.py code ran perferctly as expected. However, the receive,py code gave me the following error message:

Traceback (most recent call last):
  File "get_from_queue.py", line 10, in <module>
    channel.basic_consume(queue='hello', on_message_callback = on_message)
TypeError: basic_consume() got an unexpected keyword argument 'on_message_callback'

There isn’t much out there in terms of help regarding this type of error message in RabbitMQ or Pika.

So, I do what I always do when I have no clue, what the problem is. Unistall and reinstall.

The problem seems to be with Pika so I try to this:

Uninstall not working, problem solved

I am actually not sure why I tried this. I did install the Pika package from the Ubuntu distribution, which is called python3-pika. It should be unistalled with

sudo apt remove python3-pika

I was first trying to install via pip with

pip3 install pika

That obviously didn’t suceed because it was already installed. Then I tried the following, to see if an upgrade might help (-U)

pip3 install -U pika

This did something and after that the problem was solved. I am assuming now that python3-pika is an old package, with an api that does no longer work with the sample code on the side.

Something to be aware of…