API Reference

flask_melodramatiq.create_broker_class(classpath, *, classname=None, docstring=None, mixins=())

Create a new lazy broker class that wraps an existing broker class.

Parameters:
  • classpath – A module path to the existing broker class. For example: "dramatiq.brokers.rabbitmq:RabbitmqBroker".
  • classname – Optional name for the new class. If not given, the class name specified in classpath will be used.
  • docstring – Optional documentation string for the new class
  • mixins (tuple(type)) – Optional additional mix-in classes
Returns:

The created lazy broker class

Example:

from flask_melodramatiq import create_broker_class

PostgresBroker = create_broker_class('dramatiq_pg:PostgresBroker')
class flask_melodramatiq.Broker(app=None, config_prefix='DRAMATIQ_BROKER', **options)

A lazy broker of dynamically configurable type.

The type of the broker should be specified by the “config_prefix_CLASS” setting in the Flask application configuration. For example, if config_prefix is the defaut one, the configuration setting: DRAMATIQ_BROKER_CLASS="RedisBroker" specifies that the type of the broker should be RedisBroker.

The following broker type names are always valid:

  • "RabbitmqBroker" (default)
  • "RedisBroker"
  • "StubBroker"

In addition to these, custom broker types can be registered with create_broker_class().

Parameters:
  • app – An optonal Flask application instance
  • config_prefix – A prefix for the Flask configuration settings for this broker instance. Each broker instance should have a unique configuration settings prefix.
  • options – Keyword arguments to be passed to the constructor of the wrapped dramatiq broker class.
actor(fn=None, **kw)

Declare an actor for this broker instance.

Calls dramatiq.actor() internally.

Example:

from flask_melodramatiq import Broker

broker = Broker()

@broker.actor
def task():
    print('Snakes appreciate good theatrical preformace.')
init_app(app)

This method can be used to initialize an application for the use with this broker. A broker can not be used in the context of an application unless it is initialized that way.

init_app() is called automatically if an app argument is passed to the constructor.

set_default()

Configure this broker instance to be the global broker instance.

Calls dramatiq.set_broker() internally.

class flask_melodramatiq.RabbitmqBroker(**kwargs)

A lazy broker wrapping a RabbitmqBroker.

Parameters:
  • app – An optonal Flask application instance
  • config_prefix – A prefix for the Flask configuration settings for this broker instance. Each broker instance should have a unique configuration settings prefix.
  • options – Keyword arguments to be passed to the constructor of the wrapped dramatiq broker class.
actor(fn=None, **kw)

Declare an actor for this broker instance.

Calls dramatiq.actor() internally.

Example:

from flask_melodramatiq import Broker

broker = Broker()

@broker.actor
def task():
    print('Snakes appreciate good theatrical preformace.')
init_app(app)

This method can be used to initialize an application for the use with this broker. A broker can not be used in the context of an application unless it is initialized that way.

init_app() is called automatically if an app argument is passed to the constructor.

publish_message(message, *, exchange='', routing_key=None)

Publish a message on an exchange.

Parameters:
  • message (dramatiq.Message) – The message
  • exchange – The name of the RabbitMQ exchange on which to publish the message
  • routing_key – The message routing key. If the value is None, the routing key will be f"dramatiq.events.{message.actor_name}" if message.queue_name is None, and message.queue_name otherwise.
set_default()

Configure this broker instance to be the global broker instance.

Calls dramatiq.set_broker() internally.

class flask_melodramatiq.RedisBroker(**kwargs)

A lazy broker wrapping a RedisBroker.

Parameters:
  • app – An optonal Flask application instance
  • config_prefix – A prefix for the Flask configuration settings for this broker instance. Each broker instance should have a unique configuration settings prefix.
  • options – Keyword arguments to be passed to the constructor of the wrapped dramatiq broker class.
actor(fn=None, **kw)

Declare an actor for this broker instance.

Calls dramatiq.actor() internally.

Example:

from flask_melodramatiq import Broker

broker = Broker()

@broker.actor
def task():
    print('Snakes appreciate good theatrical preformace.')
init_app(app)

This method can be used to initialize an application for the use with this broker. A broker can not be used in the context of an application unless it is initialized that way.

init_app() is called automatically if an app argument is passed to the constructor.

set_default()

Configure this broker instance to be the global broker instance.

Calls dramatiq.set_broker() internally.

class flask_melodramatiq.StubBroker(app=None, config_prefix='DRAMATIQ_BROKER', **options)

A lazy broker wrapping a StubBroker.

Parameters:
  • app – An optonal Flask application instance
  • config_prefix – A prefix for the Flask configuration settings for this broker instance. Each broker instance should have a unique configuration settings prefix.
  • options – Keyword arguments to be passed to the constructor of the wrapped dramatiq broker class.
actor(fn=None, **kw)

Declare an actor for this broker instance.

Calls dramatiq.actor() internally.

Example:

from flask_melodramatiq import Broker

broker = Broker()

@broker.actor
def task():
    print('Snakes appreciate good theatrical preformace.')
init_app(app)

This method can be used to initialize an application for the use with this broker. A broker can not be used in the context of an application unless it is initialized that way.

init_app() is called automatically if an app argument is passed to the constructor.

set_default()

Configure this broker instance to be the global broker instance.

Calls dramatiq.set_broker() internally.