Skip to main content

Quick Start

Background

Dramatiq is a background task-processing library for Python with a focus on simplicity, reliability and performance.

This package, async-dramatiq, extends Dramatiq to provide the following:

  1. Support for Asyncio ( issue #238 )
  2. Message scheduling support ( scheduling cookbook )

Setup

To provide async support for your actors all you need to do is add the AsyncMiddleware to your broker.

RabbitMQ Broker

import dramatiq
from dramatiq.brokers.rabbitmq import RabbitmqBroker

rabbitmq_broker = RabbitmqBroker(host="rabbitmq")
rabbitmq_broker.add_middleware(AsyncMiddleware())  # <--- Here
dramatiq.set_broker(rabbitmq_broker)

Redis Broker

import dramatiq
from dramatiq.brokers.redis import RedisBroker

redis_broker = RedisBroker(host="redis")
redis_broker.add_middleware(AsyncMiddleware()) # <--- Here
dramatiq.set_broker(redis_broker)

Running

The Scheduler

We leverage apscheduler as our scheduling system. Check out run_scheduler.py for an example of running this scheduler.

Dramatiq Worker

For more details check out the official guide to dramatiq or docker-compose.yaml for a specific example.

Example

Play around with worker-heartbeat-example. A functioning and featured example implementation.


Async Middleware

AsyncMiddleware will start a AsyncWorker which will be used to run the event loop. This event loop will be shared across the Worker threads.

Startup and Shutdown Events

To startup and shutdown any resources the AsyncMiddleware provides two hooks:

  1. Before the event loop is started
  2. After the event loop is stopped To allow for standing up or tearing down of shared async resources

Example

from async_dramatiq.middleware import AsyncMiddleware

async def startup() -> None:
    """This function should contain your resource initialization code."""
    pass

async def shutdown() -> None:
    """This function should contain your resource teardown code."""
    pass


class MyAsyncMiddleware(AsyncMiddleware):
    def before_async_worker_thread_startup(
        self, _: RabbitmqBroker, thread: AsyncWorker, **kwargs: dict[str, Any]
    ) -> None:
        thread.event_loop.run_until_complete(startup())

    def after_async_worker_thread_shutdown(
        self, _: RabbitmqBroker, thread: AsyncWorker, **kwargs: dict[str, Any]
    ) -> None:
        thread.event_loop.run_until_complete(shutdown())
        thread.event_loop.close()

Async Actor

The async actor, async_actor, acts as a thin wrapper around the Dramatiq actor providing a variety of new functionality.

Interval Jobs

Run a job at some interval

@async_actor(interval=timedelta(seconds=5))
def run_every_5_seconds() -> None:
    pass

Cron Jobs

Run a job on a crontab ( See https://crontab.guru/. )

@async_actor(interval="0 0 * * *")
def run_at_midnight() -> None:
  pass

Metadata

Release files for async-dramatiq 0.1.8

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for async-dramatiq 0.1.8
File Size Uploaded Provenance
async_dramatiq-0.1.8.tar.gz 10.8 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for async-dramatiq 0.1.8
File Interpreter ABI Platform
async_dramatiq-0.1.8-py3-none-any.whl Python 3 none any Details

Total release size: 20.9 kB

Release files / async_dramatiq-0.1.8.tar.gz

Download URL async_dramatiq-0.1.8.tar.gz
Size 10.8 kB
Tags Source
SHA-256 checksum
How to use checksums
0e26ef0ee6c2697b85997007aab1ebf705e547b9b3edf1034a6bcf06477fab1a
BLAKE2b-256 checksum
How to use checksums
c3c399860188e308663b20da2ebf4e69f9dadc7c6d5710bce054d870458dce8c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.4.2 CPython/3.11.3 Darwin/21.6.0

Release files / async_dramatiq-0.1.8-py3-none-any.whl

Download URL async_dramatiq-0.1.8-py3-none-any.whl
Size 10.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
7306b40a31fb182970c0509d2c097fcb132ddb3d6a77860408e07f135cb35fc1
BLAKE2b-256 checksum
How to use checksums
5021eb416537db4ce38af133b2e264f6fcd6133d816cf8d324f9b0271c576a65
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.4.2 CPython/3.11.3 Darwin/21.6.0

Security for async-dramatiq 0.1.8

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page