As AMQP is a two-way RPC protocol where the client can send requests to the server and the server can send requests to a client, Pika implements or extends IO loops in each of its asynchronous connection adapters. These IO loops are blocking methods which loop and listen for events. Each asynchronous adapters follows the same standard for invoking the IO loop. The IO loop is created when the connection adapter is created. To start an IO loop for any given adapter, call the connection.ioloop.start() method.

If you are using an external IO loop such as Tornado’s IOLoop, you invoke it as you normally would and then add the adapter to it.

Example:

importpikadefon_open(connection):# Invoked when the connection is openpass# Create our connection object, passing in the on_open methodconnection=pika.SelectConnection(on_open_callback=on_open)try:# Loop so we can communicate with RabbitMQconnection.ioloop.start()exceptKeyboardInterrupt:# Gracefully close the connectionconnection.close()# Loop until we're fully closed, will stop on its ownconnection.ioloop.start()

Interfacing with Pika asynchronously is done by passing in callback methods you would like to have invoked when a certain event has completed. For example, if you are going to declare a queue, you pass in a method that will be called when the RabbitMQ server returns a Queue.DeclareOk response.

When we are connected, the on_connected method is called. In that method we create a channel.

When the channel is created, the on_channel_open method is called. In that method we declare a queue.

When the queue is declared successfully, on_queue_declared is called. In that method we call channel.basic_consume telling it to call the handle_delivery for each message RabbitMQ delivers to us.

When RabbitMQ has a message to send us, it call the handle_delivery method passing the AMQP Method frame, Header frame and Body.

Note

Step #1 is on line #28 and Step #2 is on line #6. This is so that Python knows about the functions we’ll call in Steps #2 through #5.

Example:

importpika# Create a global channel variable to hold our channel object inchannel=None# Step #2defon_connected(connection):"""Called when we are fully connected to RabbitMQ"""# Open a channelconnection.channel(on_channel_open)# Step #3defon_channel_open(new_channel):"""Called when our channel has opened"""globalchannelchannel=new_channelchannel.queue_declare(queue="test",durable=True,exclusive=False,auto_delete=False,callback=on_queue_declared)# Step #4defon_queue_declared(frame):"""Called when RabbitMQ has told us our Queue has been declared, frame is the response from RabbitMQ"""channel.basic_consume(handle_delivery,queue='test')# Step #5defhandle_delivery(channel,method,header,body):"""Called when we receive a message from RabbitMQ"""printbody# Step #1: Connect to RabbitMQ using the default parametersparameters=pika.ConnectionParameters()connection=pika.SelectConnection(parameters,on_connected)try:# Loop so we can communicate with RabbitMQconnection.ioloop.start()exceptKeyboardInterrupt:# Gracefully close the connectionconnection.close()# Loop until we're fully closed, will stop on its ownconnection.ioloop.start()

There are two types of connection parameter classes in Pika to allow you to pass the connection information into a connection adapter, ConnectionParameters and URLParameters. Both classes share the same default connection values.

As of RabbitMQ 2.0, client side Channel.Flow has been removed [1]. Instead, the RabbitMQ broker uses TCP Backpressure to slow your client if it is delivering messages too fast. If you pass in backpressure_detection into your connection parameters, Pika attempts to help you handle this situation by providing a mechanism by which you may be notified if Pika has noticed too many frames have yet to be delivered. By registering a callback function with the add_backpressure_callback method of any connection adapter, your function will be called when Pika sees that a backlog of 10 times the average frame size you have been sending has been exceeded. You may tweak the notification multiplier value by calling the set_backpressure_multiplier method passing any integer value.