Skip to main content

Donald

Tests Status PYPI Version Python Versions

Donald — A fast and minimal task manager for Asyncio.

Donald supports both synchronous and asynchronous functions. It can run coroutines across multiple event loops, schedule periodic tasks, and consume jobs from AMQP queues.

Key Features

  • Works with asyncio
  • Simple and lightweight API
  • Supports multiple backends: memory, redis, amqp
  • Periodic task scheduling (cron or intervals)
  • Built-in retry mechanism and failbacks
  • Can run multiple workers and schedulers in separate processes

Requirements

  • Python 3.11 or newer

Installation

Install via pip:

pip install donald

With Redis backend support:

pip install donald[redis]

Quick Start

Initialize a task manager:

import logging
from donald import Donald

# Init Donald
manager = Donald(

    # Params (default values)
    # -----------------------

    # Setup logging
    log_level=logging.INFO,
    log_config=None,

    # Choose a backend (memory|redis|amqp)
    # memory - is only recommended for testing/local development
    backend='memory',

    # Backend connection params
    # redis: {'url': 'redis://localhost:6379/0', 'channel': 'donald'}
    # amqp: {'url': 'amqp://guest:guest@localhost:5672/', 'queue': 'donald', 'exchange': 'donald'}
    backend_params={},

    # Tasks worker params
    worker_params={
      # Max tasks in work
      'max_tasks': 0,

      # Tasks default params (delay, timeout)
      'task_defaults': {},

      # A awaitable function to run on worker start
      'on_start': None

      # A awaitable function to run on worker stop
      'on_stop': None

      # A awaitable function to run on worker error
      'on_error': None
    },

    # Scheduler params
    scheduler_params={
      # Heartbeat file for scheduler healthcheck (cross-process)
      'heartbeat_file': '/tmp/donald-scheduler.heartbeat',

      # How often to update the heartbeat file (seconds)
      'heartbeat_interval': 60,
    },
)

# Wrap a function to task
@manager.task()
async def mytask(*args, **kwargs):
    # Do some job here

# Start the manager somewhere (on app start for example)
await manager.start()

# you may run a worker in the same process
# not recommended for production
worker = manager.create_worker()
worker.start()

# ...

# Submit the task to workers
mytask.submit(*args, **kwargs)

# ...

# Stop the manager when you need
await worker.stop()
await manager.stop()

Task Tuning

# Set delay and timeout
@tasks.task(delay=5, timeout=60)
async def delayed_task(*args, **kwargs):
    ...

# Automatic retries on error
@tasks.task(retries_max=3, retries_backoff_factor=2, retries_backoff_max=60)
async def retrying_task(*args, **kwargs):
    ...

# Define a failback function
@retrying_task.failback()
async def on_fail(*args, **kwargs):
    ...

# Manual retry control
@tasks.task(bind=True)
async def conditional_retry(self):
    try:
        ...
    except Exception:
        if self.retries < 3:
            self.retry()
        else:
            raise

Scheduling Tasks

@tasks.task()
async def mytask(*args, **kwargs):
    ...

# Run every 5 minutes
mytask.schedule('*/5 * * * *')

# Start the scheduler (not recommended in production)
manager.scheduler.start()

# Stop it when needed
manager.scheduler.stop()

Healthchecks

Donald provides two healthcheck methods for monitoring:

Worker healthcheck — submits a ping task and waits for a worker to respond:

# Returns True if a worker is alive and processing tasks
healthy = await manager.healthcheck(timeout=10)

Scheduler healthcheck — reads the heartbeat file written by the scheduler:

# Returns True if the scheduler process is alive and heartbeat is fresh
healthy = await manager.scheduler_healthcheck()

The scheduler writes PID and a timestamp to a heartbeat file at a configurable interval. The healthcheck reads this file and verifies:

  • The PID corresponds to a running process (via os.kill(pid, 0))
  • The last heartbeat timestamp is within heartbeat_interval * 2 seconds

This works cross-process, making it suitable for Docker HEALTHCHECK instructions regardless of the backend in use.

Docker example:

HEALTHCHECK --interval=30s --retries=3 \
  CMD python -c "import asyncio; from tasks import manager; exit(0 if asyncio.run(manager.scheduler_healthcheck()) else 1)"

To disable the heartbeat (e.g. for testing), set heartbeat_interval=0.

Running in Production

Create a task manager in tasks.py:

from donald import Donald

manager = Donald(backend='amqp')

# Define your tasks and schedules

Start a worker in a separate process:

$ donald -M tasks.manager worker

Start the scheduler (optional):

$ donald -M tasks.manager scheduler

Bug tracker

Found a bug or have a feature request? Please open an issue: 👉 https://github.com/klen/donald/issues

Contributing

Contributions are welcome! Development happens on GitHub: 🔗 https://github.com/klen/donald

License

Licensed under a MIT license.

Metadata

Release files for donald 3.2.0

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

Source distribution (sdist)

Source distribution for donald 3.2.0
File Size Uploaded Provenance Actions
donald-3.2.0.tar.gz 20.2 kB Without provenance Details for donald-3.2.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for donald 3.2.0
File Interpreter ABI Platform Actions
donald-3.2.0-py3-none-any.whl Python 3 none any Details for donald-3.2.0-py3-none-any.whl

Total release size: 37.6 kB

Release files / donald-3.2.0.tar.gz

Download URL donald-3.2.0.tar.gz
Size 20.2 kB
Tags Source
SHA-256 checksum
How to use checksums
249494e8408b51d123080c4801a5b572b0335ded34ee0f331a650edc9738d15e
BLAKE2b-256 checksum
How to use checksums
2b533de0113d0ff9dec8109273434323030e03e0ff4e30e6dffab345bd957e2b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.16

Release files / donald-3.2.0-py3-none-any.whl

Download URL donald-3.2.0-py3-none-any.whl
Size 17.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
d2e9cff7e510cdcc19ad02df608900cdc427d6085b2d025052efe9291fd67ad6
BLAKE2b-256 checksum
How to use checksums
446da975a259e86251ae6dc43efd5fa6cb2efd73c4de8b3cf98e1b1111cde140
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.13.16

Security for donald 3.2.0

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.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

3.2.0 This release

2 release files No provenance

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