Objects

class carrot.objects.BaseMessageSerializer(message=None)

A class that defines how to convert a RabbitMQ message into an executable python function from your Django project, and back again

Parameters:message (Message) – the RabbitMQ message
body()

Returns the content to be added to the RabbitMQ message body

By default, this implementation returns a simple dict in the following format:

{
    'args': ('tuple', 'of', 'positional', 'arguments'),
    'kwargs': {
        'keyword1': 'value',
        'keyword2': True
    }
}
Return type:dict
get_task(properties, body)

Identifies the python function to be executed from the content of the RabbitMQ message. By default, Carrot returns the value of the self.type_header header in the properties.

Once this string has been found, carrot uses importlib to return a callable python function.

Parameters:
  • properties – the message properties
  • body – the message body. This parameter is not used by default, but is provided so that this method can be extended to identify messages based on the content from the body, instead of the properties, in custom consumers
Returns:

a callable python function

properties()

Returns a dict from which a pika.BasicProperties object can be created

In this implementation, the following is returned: - headers - content type - priority - message id - message type

Return type:dict
publish_kwargs()

Returns a dictionary of keyword arguments to be passed to channel.basic_publish. In this implementation, the exchange, routing key and message body are returned

Return type:dict
serialize_arguments(body)

Extracts positional and keyword arguments to be sent to a function from the message body

Parameters:body (str) – the message body as a JSON string
Returns:a tuple of the positional and keyword arguments
class carrot.objects.DefaultMessageSerializer(message=None)
class carrot.objects.Message(task, virtual_host=None, queue='default', routing_key=None, exchange='', priority=0, task_args=(), task_kwargs=None)

A message to publish to RabbitMQ. Takes the following parameters:

Parameters:
  • task (str) – the path to the task to execute, e.g. myapp.mymodule.myfunction
  • virtual_host (VirtualHost) – The host containing the queue to publish the message to
  • queue (str) – the queue name. Will be set to default if not provided
  • routing_key (str) – the routing key. Defaults to the queue name
  • exchange (str) – the RabbitMQ exchange name. Can be blank (i.e. a direct exchange)
  • task_args (tuple) – positional arguments to be passed to the task
  • task_kwargs (dict) – keyword arguments to be sent to the task
  • priority (int) – a priority between 0 and 255, where 255 is the highest priority

Note

Your RabbitMQ queue must support message priority for the priority parameter to have any affect. You need to define the x-max-priority header when creating your RabbitMQ queue to do this. See Priority Queue Support for more details. Carrot applies a maximum priority of 255 by default to all queues it creates automatically.

Warning

You should not attempt to create instances of this object yourself. You should use the carrot.utilities.create_msg() function instead

connection_channel

Gets or creates the queue, and returns a tuple containing the object’s VirtualHost’s blocking connection, and its channel

publish(pika_log_level=40)

Publishes the message to RabbitMQ queue and creates a MessageLog object so the progress of the task can be tracked in the Django project’s database

Parameters:pika_log_level (logging level) – the pika log level to set. defaults to logging.ERROR
class carrot.objects.VirtualHost(url=None, host='localhost', name='%2f', port=5672, username='guest', password='guest', secure=False)

A RabbitMQ virtual host. Takes the following parameters:

Parameters:
  • host (str) – The url to the host, e.g. 192.168.0.1 or localhost
  • name (str) – The virtual host name, eg “myvirtualhost”
  • port (int) – defaults to 5672
  • username (str) – RabbitMQ username
  • password (str) – RabbitMQ password
  • secure (bool) – Whether or not to use SSL. Defaults to False
blocking_connection

Connect to the VHOST

Returns:a pika.BlockingConnection object