Getting started
Getting started
Requirements
- Python 3.10–3.14
- Flask 3.0 or 3.1
- RabbitMQ 3.13 or 4.x
Install the package:
pip install Flask-RMQApp-factory integration
Create the extension without an app so importing the module has no configuration side effects:
# extensions.py
from flask_rmq import FlaskRMQ
rmq = FlaskRMQ()Initialize it after loading configuration, then register messaging objects inside the application context:
# app.py
from flask import Flask
from flask_rmq import get_consumers_registry, get_setup_registry
from .extensions import rmq
from .messaging import consume_orders, setup_orders
def create_app() -> Flask:
app = Flask(__name__)
app.config.from_prefixed_env()
app.config["RABBITMQ_CONNECTIONS"] = {
"default": {"HOST": "localhost"},
}
rmq.init_app(app)
with app.app_context():
get_setup_registry().register(setup_orders)
get_consumers_registry().register(consume_orders)
return appRegistration is intentionally explicit. It avoids import scanning and makes startup behavior deterministic in tests, the development reloader, and production WSGI servers.
First queue
# messaging.py
from flask_rmq import Consumer, Producer
orders = Producer(queue="orders")
consume_orders = Consumer(queue="orders")
@consume_orders
def handle_order(channel, method, properties, body: bytes) -> None:
print(body.decode())
channel.basic_ack(delivery_tag=method.delivery_tag)A plain queue string tells the producer to verify it passively. Declare the queue before publishing:
def setup_orders(channel):
channel.queue_declare(queue="orders", durable=True)flask --app your_package:create_app rmq check
flask --app your_package:create_app rmq setup
flask --app your_package:create_app rmq consumePublish from a route or service:
orders.publish('{"order_id": 42}')The consumer command is a separate long-running process. In production, run the WSGI API and consumer command as separate container workloads.