Skip to main content

A Flask extension that adds support for the "dramatiq" task processing library

Project description

Flask-Melodramatiq is a Flask extension that adds support for the Dramatiq task processing library.

Dramatiq is a great library, and Flask-Melodramatiq tries hard not to force you to change the way you interact with it. Flask-Melodramatiq defines thin wrappers around the broker types available in Dramatiq, so that all the power of Dramatiq’s API remains available to you.

For example:

import dramatiq
from flask import Flask
from flask_melodramatiq import RabbitmqBroker

app = Flask(__name__)
broker = RabbitmqBroker(app)
dramatiq.set_broker(broker)

@dramatiq.actor
def task():
    print('Snakes appreciate good theatrical preformace.')

or, if you prefer the Flask application factory pattern:

import dramatiq
from flask import Flask
from flask_melodramatiq import RabbitmqBroker

broker = RabbitmqBroker()
dramatiq.set_broker(broker)

@dramatiq.actor
def task():
    print('Snakes appreciate good theatrical preformace.')

def create_app():
    app = Flask(__name__)
    broker.init_app(app)
    return app

In those examples, the broker instance that we created (we call it a “lazy broker”) is a genuine Dramatiq broker, and can be used anywhere where a “native” broker can be used. (It has dramatiq.brokers.rabbitmq.RabbitmqBroker as a superclass!) Lazy brokers are thin wrappers which add several important features:

  1. They honor the settings in the Flask application configuration.

  2. init_app can be called on them before or after the actors have been defined.

  3. The Flask application context is correctly set during the execution of the tasks.

  4. They add few convenience methods. (The Broker.actor decorator for example.)

Configuration

You can change the configuration options for your broker by passing keyword arguments to the constructor, or by setting corresponding values for the DRAMATIQ_BROKER_* set of keys in the app config. For example, you can do either:

from flask_melodramatiq import RabbitmqBroker

broker = RabbitmqBroker(
    url='amqp://mybroker:5672', confirm_delivery=True)

or you can put this in your app config:

DRAMATIQ_BROKER_URL = 'amqp://mybroker:5672'
DRAMATIQ_BROKER_CONFIRM_DELIVERY = True

If the configuration values passed to the constructor are different from the ones set in the app config, the later take precedence. You can even set/override the type of the broker in the app config:

from flask_melodramatiq import Broker

broker = Broker()  # Broker's type is not specified here.

and instead, specify the type in the app config:

DRAMATIQ_BROKER_CLASS = 'StubBroker'

This feature can be quite useful when testing your code.

Starting workers

With Flask-Melodramatiq you have the whole power of Dramatiq’s CLI on on your disposal. For example, to start worker processes for your broker instance you may run:

$ dramatiq wsgi:broker

and in wsgi.py you may have something like this:

from myapp import create_app, broker

app = create_app()

You may have as many broker instances as you want, but you need to start each one of them with a separate command.

Installation

You can install Flask-Melodramatiq with pip:

$ pip install Flask-Melodramatiq

You can read the docs here.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

Flask-Melodramatiq-1.0.1.tar.gz (11.8 kB view details)

Uploaded Source

Built Distribution

Flask_Melodramatiq-1.0.1-py3-none-any.whl (9.3 kB view details)

Uploaded Python 3

File details

Details for the file Flask-Melodramatiq-1.0.1.tar.gz.

File metadata

  • Download URL: Flask-Melodramatiq-1.0.1.tar.gz
  • Upload date:
  • Size: 11.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.9.2

File hashes

Hashes for Flask-Melodramatiq-1.0.1.tar.gz
Algorithm Hash digest
SHA256 4ea6ba057e335a114c2ee777488b2a282967bdff76a552352224da50b1536dc2
MD5 849db22db7a35f4f0e7fe079ace163c2
BLAKE2b-256 1dcd4dfc2c61497c835e26700b9ba98d5a12c6e17abb8f5e0c71629b28047fe5

See more details on using hashes here.

File details

Details for the file Flask_Melodramatiq-1.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for Flask_Melodramatiq-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 047fce32f72a16c4d6adf75fd5e8389eb000dd7e65ff13ad8a66ec9ad7e16c37
MD5 1bd5a027ae0ed6bd0c5cf9679ec40021
BLAKE2b-256 0d06f4e0be57ac98d6312a94c04a837f801e13a9f6c7c21b407d426d973240dc

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page