Flask-Dramatiq

(Cookbook, Changelog, Source Code)

Version on PyPI BSD-3-Clause Continuous Integration report

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 like dramatiq.brokers.rabbitmq.RabbitmqBroker or dramatiq.brokers.redis.RedisBroker.
  • DRAMATIQ_BROKER_URL is passed as url 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