Producer
Producer
Direct publication
from flask_rmq import Producer
producer = Producer(queue="orders")
producer.publish('{"order_id":42}')str is encoded as UTF-8. bytes passes through unchanged. Other values raise TypeError; serialize dicts explicitly so the message contract is visible.
Default BasicProperties use content_type="application/json" and delivery_mode=2. If custom properties omit delivery mode, Flask-RMQ still sets it to persistent:
from pika import BasicProperties
producer.publish(
body=b'{"order_id":42}',
properties=BasicProperties(content_type="application/json", priority=5),
)Exchange routing
events = Producer(exchange="domain.events", queue="")
events.publish(body=b"...", routing_key="orders.created")An empty queue skips queue verification. Every call uses mandatory=True; with publisher confirms enabled, an unroutable or rejected message raises instead of disappearing silently.
Queue declaration behavior
- A plain string uses
queue_declare(passive=True). The queue must already exist and may have any server-side arguments. - A
QueueConfigactively declares the queue with matching durability and DLX arguments. - Declaration is cached per actual channel. After reconnecting, the new channel verifies or declares again.
from flask_rmq import Producer, QueueConfig
queue = QueueConfig(
"orders",
dead_letter_exchange="orders.dlx",
dead_letter_routing_key="orders.failed",
)
producer = Producer(queue=queue)Decorator form
producer = Producer(queue="notifications")
@producer
def build_notification(user_id: int) -> str:
return json.dumps({"user_id": user_id})The original return value is preserved. None means “do not publish”; any other non-string/bytes type fails immediately.
Failure behavior
On reconnectable Pika transport and channel errors, the manager discards the producer channel and connection and retries the publication exactly once. The second error propagates to the caller. This bound prevents an HTTP request from retrying forever; use an application-level transactional outbox when publication must survive broker downtime.