Flask-Dramatiq¶
(Cookbook, Changelog, Source Code)
Welcome to Flask-Dramatiq’s documentation. Flask-Dramatiq is a Flask extensions to bridge Dramatiq task queue in your web app.
Features¶
- Setup Dramatiq from Flask configuration.
- Ensure Flask app is available to Dramatiq actor.
- Add
worker
command to Flask CLI. - Enable Flask Application factory.
- Handle multiple brokers with configurable prefix.
- Automatic code reload on change if
DEBUG
is set.
Get started¶
Flask-Dramatiq is licensed under BSD-3-Clause. Add flask-dramatiq
package to
your project:
$ poetry add flask-dramatiq
Then use Dramatiq
object as a regular Flask extension. The Dramatiq
object replaces the dramatiq
module.
from flask import Flask
from flask_dramatiq import Dramatiq
app = Flask(__name__)
dramatiq = Dramatiq(app)
@dramatiq.actor()
def my_actor():
...
@app.route("/")
def myhandler():
my_actor.send()
Flask-Dramatiq adds two configuration keys:
DRAMATIQ_BROKER
, points to broker class likedramatiq.brokers.rabbitmq:RabbitmqBroker
ordramatiq.brokers.redis:RedisBroker
.DRAMATIQ_BROKER_URL
is passed asurl
keyword argument to Dramatiq broker class.
Now run worker command to consume messages and execute tasks in the background:
$ flask worker --processes=1
That’s it! A complete flask app is available in project source tree example.py