Search code examples
rabbitmqpikapython-pika

RabbitMQ def callback(ch, method, properties, body)


Just want to know the meaning of the parameters in worker.py file:

def callback(ch, method, properties, body):
    print " [x] Received %r" % (body,)

What do ch, method, and properties mean?


Solution

  • ch

    "ch" is the "channel" over which the communication is happening.

    Think of a RabbitMQ connection in two parts:

    • the TCP/IP connection
    • channels within the connection

    the actual TCP/IP connection is expensive to create, so you only want one connection per process instance.

    A channel is where the work is done with RabbitMQ. a channel exists within an connection, and you need to have the channel reference so you can ack/nack messages, etc.

    method

    i think "method" is meta information regarding the message delivery

    when you want to acknowledge the message - tell RabbitMQ that you are done processing it - you need both the channel and the delivery tag. the delivery tag comes from the method parameter.

    i'm not sure why this is called "method" - perhaps it is related to the AMQP spec, where the "method" is meta-data about which AMQP method was executed?

    properties

    the "properties" of the message are user-defined properties on the message. you can set any arbitrary key / value pair that you want in these properties, and possibly get things like routing key used (though this may come from "method")

    properties are often uses for bits of data that your code needs to have, but aren't part of the actual message body.

    for example, if you had a re-sequencer process to make sure messages are processed in order, the "properties" would probably contain the message's sequence number.