The recommended library for Python to access RabbitMQ servers is Pika.
The following code connects to CloudAMQP, declares a queues, publish a message to it, setups a subscription and print messages coming to the queue.
Note: The DEFAULT_SOCKET_TIMEOUT is set to 0.25s, we would recommend to raise
this parameter to about 5s to avoid connection timeout,
params.socket_timeout = 5
Other connection parameter options for Pika can be found here:
The full code can be seen at github.com/cloudamqp/python-amqp-example.
import pika, os, urlparse, logging logging.basicConfig() # Access the CLODUAMQP_URL environment variable and parse it (fallback to localhost) url = os.environ.get('CLOUDAMQP_URL', 'amqp://guest:guest@localhost/%2f') params = pika.URLParameters(url) params.socket_timeout = 5 connection = pika.BlockingConnection(params) # Connect to CloudAMQP channel = connection.channel() # start a channel channel.queue_declare(queue='hello') # Declare a queue # send a message channel.basic_publish(exchange='', routing_key='hello', body='Hello CloudAMQP!') print " [x] Sent 'Hello World!'" # create a function which is called on incoming messages def callback(ch, method, properties, body): print " [x] Received %r" % (body) # set up subscription on the queue channel.basic_consume(callback, queue='hello', no_ack=True) channel.start_consuming() # start consuming (blocks) connection.close()
Information about Celery can be found here.
Here's a video recording showing how to setup a CloudAMQP instance and connect to it using Python and the Pika library: