Skip to main content

Django Tasks

CI PyPI PyPI - Python Version PyPI - Status PyPI - License

An backport of django.tasks - Django's built-in Tasks framework.

Installation

python -m pip install django-tasks

The first step is to add django_tasks to your INSTALLED_APPS.

INSTALLED_APPS = [
    # ...
    "django_tasks",
]

Secondly, you'll need 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:

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

Prior to 0.12.0, django-tasks-db and django-tasks-rq were also included to provide database and RQ based backends.

Usage

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 (between -100 and 100. Larger numbers are higher priority. 0 by default)
  • 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)
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.

Task context

Sometimes the running task may need to know context about how it was enqueued. To receive the task context as an argument to your task function, pass takes_context to the decorator and ensure the task takes a context as the first argument.

from django_tasks import task, TaskContext


@task(takes_context=True)
def calculate_meaning_of_life(context: TaskContext) -> int:
    return 42

The task context has the following attributes:

  • task_result: The running task result
  • attempt: The current attempt number for the task

This API will be extended with additional features in future.

Enqueueing 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.

Queue names

By default, tasks are enqueued onto the "default" queue. When using multiple queues, it can be useful to constrain the allowed names, so tasks aren't missed.

TASKS = {
    "default": {
        "BACKEND": "django_tasks.backends.immediate.ImmediateBackend",
        "QUEUES": ["default", "special"]
    }
}

Enqueueing tasks to an unknown queue name raises InvalidTaskError.

To disable queue name validation, set QUEUES to [].

Retrieving task result

When enqueueing a task, you get a TaskResult, however it may be useful to retrieve said result from somewhere else (another request, another task etc). This can be done with get_result (or aget_result):

result_id = result.id

# Later, somewhere else...
calculate_meaning_of_life.get_result(result_id)

A result id should be considered an opaque string, whose length could be up to 64 characters. ID generation is backend-specific.

Only tasks of the same type can be retrieved this way. To retrieve the result of any task, you can call get_result on the backend:

from django_tasks import default_task_backend

default_task_backend.get_result(result_id)

Return values

If your task returns something, it can be retrieved from the .return_value attribute on a TaskResult. Accessing this property on an unsuccessful task (ie not SUCCESSFUL) will raise a ValueError.

assert result.status == TaskResultStatus.SUCCESSFUL
assert result.return_value == 42

If a result has been updated in the background, you can call refresh on it to update its values. Results obtained using get_result will always be up-to-date.

assert result.status == TaskResultStatus.READY
result.refresh()
assert result.status == TaskResultStatus.SUCCESSFUL

Errors

If a task raised an exception, its .errors contains information about the error:

assert result.errors[0].exception_class == ValueError

Note that this is just the type of exception, and contains no other values. The traceback information is reduced to a string that you can print to help debugging:

assert isinstance(result.errors[0].traceback, str)

Note that currently, whilst .errors is a list, it will only ever contain a single element.

Attempts

The number of times a task has been run is stored as the .attempts attribute. This will currently only ever be 0 or 1.

The date of the last attempt is stored as .last_attempted_at.

Backend introspecting

Because django-tasks enables support for multiple different backends, those backends may not support all features, and it can be useful to determine this at runtime to ensure the chosen task queue meets the requirements, or to gracefully degrade functionality if it doesn't.

  • supports_defer: Can tasks be enqueued with the run_after attribute?
  • supports_async_task: Can coroutines be enqueued?
  • supports_get_result: Can results be retrieved after the fact (from any thread / process)?
  • supports_priority: Can tasks be executed in a given priority order?
from django_tasks import default_task_backend

assert default_task_backend.supports_get_result

This is particularly useful in combination with Django's system check framework.

Signals

A few Signals are provided to more easily respond to certain task events.

Whilst signals are available, they may not be the most maintainable approach.

  • django_tasks.signals.task_enqueued: Called when a task is enqueued. The sender is the backend class. Also called with the enqueued task_result.
  • django_tasks.signals.task_finished: Called when a task finishes (SUCCESSFUL or FAILED). The sender is the backend class. Also called with the finished task_result.
  • django_tasks.signals.task_started: Called immediately before a task starts executing. The sender is the backend class. Also called with the started task_result.

Contributing

See CONTRIBUTING.md for information on how to contribute.

Metadata

Release files for django-tasks 0.12.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 django-tasks 0.12.0
File Size Uploaded
django_tasks-0.12.0.tar.gz 15.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for django-tasks 0.12.0
File Interpreter ABI Platform
django_tasks-0.12.0-py3-none-any.whl Python 3 none any Details

Total release size: 31.2 kB

Release files / django_tasks-0.12.0.tar.gz

Download URL django_tasks-0.12.0.tar.gz
Size 15.4 kB
Tags Source
SHA-256 checksum
How to use checksums
58be66c1e487da32a3ce7320bd1949d0d1dc381b9004819f92591eb37fb2c1b8
BLAKE2b-256 checksum
How to use checksums
1d6e34d4e77bb7951e5a5acbd846f43240f062c68bb04d9c246cb446a55d4cbc
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Feb 6, 2026.

Transparency log

Release files / django_tasks-0.12.0-py3-none-any.whl

Download URL django_tasks-0.12.0-py3-none-any.whl
Size 15.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ffcc1d7bdfad3bc5ef9c2d498596c6546484c9ec6b182fb9709c691eb66a8709
BLAKE2b-256 checksum
How to use checksums
07e740c45768e84efd77e3ae6ffdcd2b5540011036cbb00c6df6ef2a2ad69551
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.13.7

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Feb 6, 2026.

Transparency log

Release history Release notifications | RSS feed

This release

0.12.0 This release

2 release files

0.9.0

2 release files

0.8.1

2 release files

0.8.0

2 release files

0.7.0

2 release files

0.6.1

2 release files

0.6.0

2 release files

0.5.0

2 release files

0.4.0

2 release files

0.3.0

2 release files

0.2.0

2 release files

0.1.1

2 release files

0.1.0

2 release files

0.0.0

2 release files

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