Skip to main content

A reference implementation and backport of background workers and tasks in Django

Project description

Django Tasks

CIPyPI PyPI - Python Version PyPI - Status PyPI - License

A reference implementation and backport of background workers and tasks in Django, as defined in DEP 0014.

Warning: This code is not intended to be installed, let alone deployed to production!

Installation

pip install django-tasks

Usage

Note: This documentation is still work-in-progress. Further details can also be found on the DEP. The tests are also a good exhaustive reference.

Settings

The first step is to configure a backend. This connects the tasks to whatever is going to execute them.

If omitted, the following configuration is used:

TASKS = {
    "default": {
        "BACKEND": "django_tasks.backends.immediate.ImmediateBackend"
    }
}

A few backends are included by default:

  • DummyBackend: Don't execute the tasks, just store them. This is especially useful for testing.
  • ImmediateBackend: Execute the task immediately in the current thread

Defining tasks

A task is created with the task decorator.

from django_tasks import task


@task()
def calculate_meaning_of_life() -> int:
    return 42

The task decorator accepts a few arguments to customize the task:

  • priority: The priority of the task (larger numbers are higher priority)
  • queue_name: Whether to run the task on a specific queue
  • backend: Name of the backend for this task to use (as defined in TASKS)

These attributes can also be modified at run-time with .using:

modified_task = calculate_meaning_of_life.using(priority=10)

In addition to the above attributes, run_after can be passed to specify a specific time the task should run. Both a timezone-aware datetime or timedelta may be passed.

Executing tasks

To execute a task, call the enqueue method on it:

result = calculate_meaning_of_life.enqueue()

The returned TaskResult can be interrogated to query the current state of the running task, as well as its return value.

If the task takes arguments, these can be passed as-is to enqueue.

Sending emails

To make sending emails simpler, a backend is provided to automatically create tasks for sending emails via SMTP:

EMAIL_BACKEND = "django_tasks.mail.SMTPEmailBackend"

SMTP credentials are configured as usual.

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

django_tasks-0.0.0.tar.gz (11.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

django_tasks-0.0.0-py3-none-any.whl (12.4 kB view details)

Uploaded Python 3

File details

Details for the file django_tasks-0.0.0.tar.gz.

File metadata

  • Download URL: django_tasks-0.0.0.tar.gz
  • Upload date:
  • Size: 11.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.0.0 CPython/3.12.3

File hashes

Hashes for django_tasks-0.0.0.tar.gz
Algorithm Hash digest
SHA256 f400f3c8a1578f5546a01c45a6a1e827ff9a22ac2fae85d54215bbef28a5f7b1
MD5 eec16ccaf6f086b20c6da2ba683e9aba
BLAKE2b-256 bab3faaa8336ebb8cd18257ae6e03d4c1a2d6dc43728b15b5c3aa433b85342b8

See more details on using hashes here.

File details

Details for the file django_tasks-0.0.0-py3-none-any.whl.

File metadata

  • Download URL: django_tasks-0.0.0-py3-none-any.whl
  • Upload date:
  • Size: 12.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.0.0 CPython/3.12.3

File hashes

Hashes for django_tasks-0.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 cd8bae4363984f890179608eca46ef5fd352ab6ca106ff5f3648112284105d30
MD5 38d9db37ba79dd082173270d7b066876
BLAKE2b-256 5e022cad7587e6db7541805b4d72f19bc635998b35b72dfc28c011f2cc21edb7

See more details on using hashes here.

Supported by

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