1

I am new to python and pika and I am running into problems consuming from a queue using the BlockingConnection adapter which keeps throwing an exception after a few hours.

I am therefor now trying to use the SelectConnection (Asynchronous) adapter but I can only find examples that use this adapter type within a class and working with the class based code is a little beyond my understanding at the moment.

I did find an example that shows how to create a producer using SelectConnection but I can not find an example for a consumer which is pissing me off no end as I would have thought the pika website would detail both a basic producer and consumer rather than just a producer...

The producer code is below and is taken from the pika website (why they did not include an example for a basic consumer is beyond me...): (http://pika.readthedocs.io/en/latest/examples/comparing_publishing_sync_async.html)

import pika

# Step #3
def on_open(connection):

    connection.channel(on_channel_open)

# Step #4
def on_channel_open(channel):

    channel.basic_publish('test_exchange',
                            'test_routing_key',
                            'message body value',
                            pika.BasicProperties(content_type='text/plain',
                                                 delivery_mode=1))

    connection.close()

# Step #1: Connect to RabbitMQ
parameters = pika.URLParameters('amqp://guest:guest@localhost:5672/%2F')

connection = pika.SelectConnection(parameters=parameters,
                                   on_open_callback=on_open)

try:

Step #2 - Block on the IOLoop
connection.ioloop.start()

Catch a Keyboard Interrupt to make sure that the connection is closed cleanly
except KeyboardInterrupt:

# Gracefully close the connection
connection.close()

# Start the IOLoop again so Pika can communicate, it will stop on its own when the connection is closed
connection.ioloop.start()

Can anyone advise on how I can amend this code to 'consume' rather than 'produce' or can you point me to any examples that just use basic functions rather than the class based examples which I have found many examples of but are of no use for my particular purpose...

Thank you. (as you may have gathered from the tone of my question, I am a little stressed at the moment as its 4am and I have been trying to resolve this for hours!)

1 Answers1

2

in case this helps anyone one else and after a lot of messing about...

http://rabbitpy.readthedocs.io

Much simpler and seems very fast...