CloudAMQP with Python Getting started

The recommended library for Python to access RabbitMQ servers is Pika.

Put pika==0.9.14 in your requirement.txt file.

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: Connection Parameters.

The full code can be seen at

import pika, os, urlparse, logging

# 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 = # 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.start_consuming() # start consuming (blocks)


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:

Alternative clients

Rabbitpy is a simple and easy to use API for interfacing with RabbitMQ. It is a pure python, thread-safe, minimalistic and pythonic RabbitMQ client library. More information about Rabbitpy can be found here.