Skip to main content
Version: 1.1.6

API Reference

Flat reference for every public symbol in django_rmq. Signatures and parameter descriptions are taken directly from the source code.


Producer​

from django_rmq.producer import Producer

Publishes messages to RabbitMQ through a thread-local blocking channel.

Producer.__init__​

def __init__(
self,
exchange: str = '',
queue: QueueConfig | str = '',
using: str | None = None,
) -> None
ParameterTypeDefaultDescription
exchangestr''Exchange name. Empty string means the default (direct) exchange.
queueQueueConfig | str''Queue configuration or name. Used as default routing key; declared lazily on first publish. Empty string disables queue declaration (exchange-only mode).
usingstr | NoneNoneConnection alias from RABBITMQ_CONNECTIONS. May be omitted when exactly one alias is configured.

Producer.publish​

def publish(
self,
body: str | bytes,
routing_key: str | None = None,
properties: BasicProperties | None = None,
) -> None
ParameterTypeDefaultDescription
bodystr | bytesrequiredMessage body. Strings are encoded to bytes (UTF-8).
routing_keystr | NoneNoneRouting key. Defaults to self.queue (the queue name) when not provided.
propertiesBasicProperties | NoneNoneAMQP message properties. When omitted, created with content_type='application/json'. delivery_mode is always forced to 2 (persistent).

Returns: None

Raises:

  • pika.exceptions.UnroutableError — broker returned the message because no queue matched the routing key (requires publisher confirms + mandatory=True, both always enabled).
  • pika.exceptions.NackError — broker nacked the message.
  • Any exception from _RECONNECTABLE_ERRORS if both the first attempt and the single retry fail.

Producer.__call__​

Allows a Producer instance to be used as a function decorator. The decorated function must return str, bytes, or None. The return value is published automatically; None skips publishing.

def __call__(
self,
func: Callable[..., str | bytes | None],
) -> Callable[..., str | bytes | None]

Raises: TypeError if the decorated function returns a type other than str, bytes, or None.

Example:

from django_rmq.producer import Producer

producer: Producer = Producer(queue='notifications')


@producer
def build_notification(user_id: int) -> str:
return f'{{"user_id": {user_id}}}'

Consumer​

from django_rmq.consumer import Consumer

Consumes messages from a single RabbitMQ queue with one registered handler. Reconnects on transient AMQP errors with exponential backoff.

Consumer.__init__​

def __init__(
self,
queue: QueueConfig | str,
prefetch_count: int = 1,
reconnect_initial_backoff: float | None = None,
reconnect_max_backoff: float | None = None,
using: str | None = None,
) -> None
ParameterTypeDefaultDescription
queueQueueConfig | strrequiredQueue to consume from. QueueConfig triggers an active declare with arguments; a plain string triggers a durable declare.
prefetch_countint1Maximum unacknowledged messages delivered at once (basic_qos).
reconnect_initial_backofffloat | NoneNoneInitial reconnect delay in seconds. Falls back to the alias config value when None.
reconnect_max_backofffloat | NoneNoneMaximum reconnect delay in seconds (exponential backoff cap). Falls back to alias config when None.
usingstr | NoneNoneConnection alias. May be omitted when exactly one alias is configured.

Consumer.handler​

def handler(self, func: MessageCallback) -> MessageCallback

Registers a callback for incoming messages. Can be used as a decorator.

Raises: RuntimeError if a handler is already registered on this consumer.

Consumer.__call__​

Shorthand for @consumer.handler. Identical behavior.

def __call__(self, func: MessageCallback) -> MessageCallback

Consumer.consume​

def consume(self, stop_event: threading.Event | None = None) -> None

Starts the consume loop. Reconnects on transient errors. Exits when stop_event is set or an unrecoverable error is raised.

ParameterTypeDefaultDescription
stop_eventthreading.Event | NoneNoneEvent that signals graceful shutdown. When None, an internal event (never set) is created — the consumer runs until an error.

Consumer properties​

PropertyTypeDescription
prefetch_countintMaximum unacknowledged messages delivered at once.
usingstr | NoneConnection alias, or None when the single alias is used implicitly.
handler_namestrName of the registered handler function, or 'unregistered' if none.

MessageCallback​

from django_rmq.consumer import MessageCallback

Type alias for the handler callable signature:

MessageCallback = Callable[
[BlockingChannel, Basic.Deliver, BasicProperties, bytes],
None,
]
ArgumentTypeDescription
chBlockingChannelChannel the message was delivered on. Used to call basic_ack / basic_nack.
methodBasic.DeliverDelivery metadata, including delivery_tag.
propsBasicPropertiesAMQP message properties.
bodybytesRaw message body.

QueueConfig​

from django_rmq.queues.queue_config import QueueConfig,

QueueType

Frozen dataclass for declarative queue configuration.

@dataclass(frozen=True)
class QueueConfig:
name: str
durable: bool = True
queue_type: QueueType | None = None
dead_letter_exchange: str | None = None
dead_letter_routing_key: str | None = None
FieldTypeDefaultDescription
namestrrequiredQueue name. Also used as str(queue_config).
durableboolTrueQueue survives broker restarts.
queue_typeQueueType | NoneNoneSets x-queue-type on declaration. None uses the broker's default_queue_type.
dead_letter_exchangestr | NoneNoneExchange dead-lettered messages are routed to (x-dead-letter-exchange).
dead_letter_routing_keystr | NoneNoneRouting key for dead-lettered messages (x-dead-letter-routing-key).

QueueType​

Enum of the RabbitMQ queue types django_rmq knows how to declare; its value is serialized to the broker as the x-queue-type argument.

MemberValue
CLASSIC'classic'
QUORUM'quorum'
STREAM'stream'

QueueType subclasses str (instead of the 3.11+ StrEnum, unavailable on Python 3.10), so each member is an actual string and serializes to the broker as-is. When queue_type is left unset on a QueueConfig, the broker applies its own default_queue_type.

QueueConfig.arguments property​

@property
def arguments(self) -> dict[str, Any] | None

Builds the AMQP arguments dict for queue declaration from queue_type (x-queue-type) and the dead-letter fields. Returns None when none of them are set.


RabbitMQConfig​

from django_rmq.dto.rabbitmq_config import RabbitMQConfig

Frozen dataclass holding the resolved configuration for one connection alias. Created internally by RabbitMQAppConfig.ready() from the RABBITMQ_CONNECTIONS setting. Exposed on RabbitMQConnectionManager.config.

@dataclass(frozen=True)
class RabbitMQConfig:
nodes: tuple[NodeConfig, ...]
virtual_host: str
user: str
password: str
heartbeat: int
blocked_connection_timeout: int
reconnect_initial_backoff: float
reconnect_max_backoff: float
shuffle_nodes: bool = False
socket_timeout: float = 10.0
stack_timeout: float = 15.0

An alias may point at one node (a plain broker) or several (a cluster). All nodes share the same virtual host and credentials.

FieldTypeDefaultDescription
nodestuple[NodeConfig, ...]requiredOne or more node addresses. pika tries them in order until one accepts the connection (client-side failover).
virtual_hoststrrequiredAMQP virtual host to connect to.
userstrrequiredUsername for PlainCredentials.
passwordstrrequiredPassword for PlainCredentials.
heartbeatintrequiredHeartbeat interval in seconds negotiated with the broker.
blocked_connection_timeoutintrequiredSeconds to wait while the broker blocks the connection before failing.
reconnect_initial_backofffloatrequiredInitial consumer reconnect delay in seconds.
reconnect_max_backofffloatrequiredMaximum consumer reconnect delay in seconds (cap for the exponential backoff).
shuffle_nodesboolFalseReshuffle the node order on every connection attempt so clients spread across the cluster.
socket_timeoutfloat10.0Seconds to wait for the TCP connection to a single node (pika socket_timeout).
stack_timeoutfloat15.0Seconds to wait for the full TCP + AMQP bring-up of a single node (pika stack_timeout).

NodeConfig​

from django_rmq.dto.rabbitmq_config import NodeConfig
@dataclass(frozen=True)
class NodeConfig:
host: str
port: int

Immutable address of a single RabbitMQ node within a connection alias.

FieldTypeDescription
hoststrNode hostname or IP address.
portintNode AMQP port.

HealthcheckConfig​

from django_rmq.dto.healthcheck_config import HealthcheckConfig
@dataclass(frozen=True)
class HealthcheckConfig:
deadline: float = 15.0
socket_timeout: float = 5.0
stack_timeout: float = 10.0
FieldTypeDefaultSetting keyDescription
deadlinefloat15.0DEADLINEOverall budget in seconds for checking every alias. When it expires, aliases without a live node are reported as failed.
socket_timeoutfloat5.0SOCKET_TIMEOUTSeconds to wait for the TCP connection to a single node.
stack_timeoutfloat10.0STACK_TIMEOUTSeconds to wait for the full TCP + AMQP bring-up of a single node.

RabbitMQConnectionManager​

from django_rmq.connections import RabbitMQConnectionManager

Manages thread-local connections for producer and consumer roles. Instantiated once per alias by RabbitMQAppConfig.ready().

RabbitMQConnectionManager.__init__​

def __init__(self, config: RabbitMQConfig) -> None:

Builds one ConnectionParameters per node, each with the alias socket_timeout and stack_timeout.

RabbitMQConnectionManager.get_producer_connection​

def get_producer_connection(self) -> BlockingConnection

Returns the thread-local BlockingConnection used by producers on the current thread. Creates it on first call; reuses the cached instance while it is open.

RabbitMQConnectionManager.get_consumer_connection​

def get_consumer_connection(self) -> BlockingConnection

Returns the thread-local BlockingConnection used by consumers on the current thread. Kept separate from the producer connection to allow safe publishing from inside a handler.

RabbitMQConnectionManager.get_producer_channel​

def get_producer_channel(self) -> BlockingChannel

Returns the thread-local producer channel with publisher confirms enabled (confirm_delivery()). Creates it on first call; reuses while open.

RabbitMQConnectionManager.reset_producer_channel​

def reset_producer_channel(self) -> None

Closes and discards the cached producer channel and connection. Called automatically by Producer.publish after a reconnectable error so the next publish opens a fresh channel. Safe to call even when no channel is cached.

RabbitMQConnectionManager.build_probe_parameters​

def build_probe_parameters(
self,
socket_timeout: float,
stack_timeout: float,
) -> list[ConnectionParameters]
ParameterTypeDefaultDescription
socket_timeoutfloatrequiredSeconds to wait for the TCP connection to a node.
stack_timeoutfloatrequiredSeconds to wait for the full TCP + AMQP bring-up of a node.

Returns: list[ConnectionParameters] — one entry per node, in NODES order.

Builds independent per-node connection parameters for probing nodes one by one. The order follows NODES and is never shuffled; a new list is built on every call and nothing is cached. No connection is opened. Credentials, virtual host, heartbeat, and blocked-connection timeout are the same as for the alias; the alias socket_timeout / stack_timeout and shuffle_nodes are ignored. Used by check_rabbitmq_connections.


get_connection_manager​

from django_rmq.connections import get_connection_manager
def get_connection_manager(using: str | None = None) -> RabbitMQConnectionManager

Returns the RabbitMQConnectionManager for the given alias.

ParameterTypeDefaultDescription
usingstr | NoneNoneAlias to resolve. May be omitted when exactly one alias is configured.

Raises: ImproperlyConfigured — alias not found, ambiguous (multiple aliases, using omitted), or django_rmq not initialized.


ConsumersRegistry​

from django_rmq.registries.registry import ConsumersRegistry

Holds the consumers registered for one connection alias.

ConsumersRegistry.register​

def register(self, consumer: Consumer) -> None

Adds a consumer to the registry.

ConsumersRegistry.all​

def all(self) -> list[Consumer]

Returns a copy of all registered consumers. Mutating the returned list does not affect the registry.


get_consumers_registry​

from django_rmq.registries.registry import get_consumers_registry
def get_consumers_registry(using: str | None = None) -> ConsumersRegistry

Returns the ConsumersRegistry for the given alias.

Raises: ImproperlyConfigured — same conditions as get_connection_manager.


SetupRegistry​

from django_rmq.registries.setup_registry import SetupRegistry

Holds idempotent topology-setup functions for one connection alias.

SetupRegistry.register​

def register(self, fn: SetupFn) -> None

Adds a setup function to the registry. Functions are invoked in registration order.

SetupRegistry.run_all​

def run_all(self, channel: BlockingChannel) -> None

Runs every registered setup function on the given channel in registration order.


get_setup_registry​

from django_rmq.registries.setup_registry import get_setup_registry
def get_setup_registry(using: str | None = None) -> SetupRegistry

Returns the SetupRegistry for the given alias.

Raises: ImproperlyConfigured — same conditions as get_connection_manager.


SetupFn​

from django_rmq.registries.setup_registry import SetupFn

Type alias for a topology setup callable:

SetupFn = Callable[[BlockingChannel], None]

A SetupFn receives an open BlockingChannel and must be idempotent (safe to call multiple times with the same outcome).


RabbitMQAppConfig​

from django_rmq.apps import RabbitMQAppConfig

Django AppConfig for django_rmq. Registered automatically when 'django_rmq' is in INSTALLED_APPS.

RabbitMQAppConfig.ready​

def ready(self) -> None

Reads RABBITMQ_CONNECTIONS from Django settings and builds, per alias, one RabbitMQConnectionManager, one SetupRegistry, and one ConsumersRegistry. Stores them on the django_rmq module as connection_managers, setup_registries, and consumers_registries. Also parses the optional RABBITMQ_HEALTHCHECK setting into django_rmq.healthcheck_config, so its errors surface when the worker starts rather than in the healthcheck probe.

Raises: ImproperlyConfigured — RABBITMQ_CONNECTIONS is missing or empty, node addressing is missing or invalid, SOCKET_TIMEOUT / STACK_TIMEOUT is not a positive number, or RABBITMQ_HEALTHCHECK is invalid. See the Error cases in the Configuration guide.


Module globals (django_rmq)​

import django_rmq

django_rmq.connection_managers # dict[str, RabbitMQConnectionManager] | None
django_rmq.setup_registries # dict[str, SetupRegistry] | None
django_rmq.consumers_registries # dict[str, ConsumersRegistry] | None
django_rmq.healthcheck_config # HealthcheckConfig | None

All four are None until RabbitMQAppConfig.ready() runs. Use the accessor functions (get_connection_manager, get_consumers_registry, get_setup_registry) rather than accessing the first three dicts directly. There is no accessor for healthcheck_config; read it from the module (check_rabbitmq_connections falls back to HealthcheckConfig() when it is None).


Management commands​

setup_rabbitmq_topology​

uv run python manage.py setup_rabbitmq_topology [--using ALIAS]

Declares all exchanges, queues, and bindings registered in SetupRegistry for one or all aliases. Idempotent — safe to run on every deploy.

OptionDescription
--using ALIASRun only for the named alias. Omit to run for every alias.

After setup it prints a report of declared exchanges, queues, and bindings, de-duplicated by name.

check_rabbitmq_connections​

uv run python manage.py check_rabbitmq_connections [--using ALIAS] [--format {json,text}] [--timeout SECONDS] [--skip-checks]

Healthcheck for container probes. Probes every node of one or all aliases in parallel (opens a connection and closes it immediately) with the timeouts from RABBITMQ_HEALTHCHECK. An alias is healthy as soon as one of its nodes answers; aliases without a live node fail when all nodes fail or the deadline expires. Exits with code 0 when every alias is healthy and 1 otherwise.

OptionDescription
--using ALIASCheck only the named alias. Omit to check every alias.
--format {json,text}Output format: text (default, logfmt-style key=value) or json (one object per line).
--timeout SECONDSOverall deadline in seconds. Must be greater than 0. Defaults to RABBITMQ_HEALTHCHECK['DEADLINE'] (15.0).
--skip-checksNo-op, kept for backward compatibility: the command never runs system checks.

See Management Commands for output events, exit codes, and examples.

start_consumers​

uv run python manage.py start_consumers [--using ALIAS]

Starts every consumer registered in ConsumersRegistry for one or all aliases. Each consumer runs in its own thread sharing a single stop_event. SIGTERM and SIGINT set the event for graceful shutdown; the command joins all threads before returning.

OptionDescription
--using ALIASStart consumers for the named alias only. Omit for all aliases.

Settings reference​

Keys of each alias in RABBITMQ_CONNECTIONS:

KeyTypeRequiredDescription
HOSTstrwith PORT, or use NODESBroker hostname or IP. Mutually exclusive with NODES.
PORTintwith HOST, or use NODESAMQP port (typically 5672). Mutually exclusive with NODES.
NODESlist[dict[str, Any]]or use HOST / PORTNon-empty list of nodes, each with HOST and PORT. Mutually exclusive with HOST / PORT.
VIRTUAL_HOSTstryesVirtual host (e.g. '/').
USERstryesAuthentication username.
PASSWORDstryesAuthentication password.
HEARTBEATintyesHeartbeat interval in seconds.
BLOCKED_CONNECTION_TIMEOUTintyesSeconds before a blocked connection times out.
RECONNECT_INITIAL_BACKOFFfloatyesInitial consumer reconnect delay in seconds.
RECONNECT_MAX_BACKOFFfloatyesMaximum consumer reconnect delay (backoff cap).
SHUFFLE_NODESboolno (default False)Reshuffle the node order on every connection attempt.
SOCKET_TIMEOUTfloatno (default 10.0)Seconds to wait for the TCP connection to one node. Positive number; bool and strings are rejected.
STACK_TIMEOUTfloatno (default 15.0)Seconds to wait for the full TCP + AMQP bring-up of one node. Positive number; bool and strings are rejected.

Required keys: VIRTUAL_HOST, USER, PASSWORD, HEARTBEAT, BLOCKED_CONNECTION_TIMEOUT, RECONNECT_INITIAL_BACKOFF, RECONNECT_MAX_BACKOFF. A missing key raises a KeyError during AppConfig.ready(). Node addressing is required too: either HOST and PORT, or NODES (not both); otherwise ImproperlyConfigured is raised. SHUFFLE_NODES, SOCKET_TIMEOUT and STACK_TIMEOUT are optional.

The optional global RABBITMQ_HEALTHCHECK setting is a dict; every key is optional and {} keeps all defaults:

KeyTypeDefaultDescription
DEADLINEfloat15.0Overall budget in seconds for checking every alias.
SOCKET_TIMEOUTfloat5.0Seconds to wait for the TCP connection to one node during the probe.
STACK_TIMEOUTfloat10.0Seconds to wait for the full TCP + AMQP bring-up of one node.

See Configuration for a full settings example.