Skip to main content

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

Project description

Django Tasks

CI PyPI 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 package is under active development, and breaking changes may be released at any time. Be sure to pin to specific versions (even patch versions) if you're using this package in a production environment.

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
  • DatabaseBackend: Store tasks in the database (via Django's ORM), and retrieve and execute them using the db_worker management command

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.

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.

Executing tasks with the database backend

First, you'll need to add django_tasks.backends.database to INSTALLED_APPS, and run manage.py migrate.

Next, configure the database backend:

TASKS = {
    "default": {
        "BACKEND": "django_tasks.backends.database.DatabaseBackend"
    }
}

Finally, you can run manage.py db_worker to run tasks as they're created. Check the --help for more options.

[!CAUTION] The database backend does not work with SQLite when you are running multiple worker processes - tasks may be executed more than once. See #33.

Contributing

See CONTRIBUTING.md for information on how to contribute.

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.1.1.tar.gz (16.8 kB view details)

Uploaded Source

Built Distribution

django_tasks-0.1.1-py3-none-any.whl (20.4 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for django_tasks-0.1.1.tar.gz
Algorithm Hash digest
SHA256 86fa574f504cf4597ca8b929143b57c87412e82021141b7ec90df112458bf3f5
MD5 9e63736e8cc41b8914eef690dfcdc617
BLAKE2b-256 eb04f1d5d5c7363f9839e0b7337945a2c7d5cb40f902db8c145f71cc9473c652

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for django_tasks-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 832a377302e2df225c0ca139cf0ebb24e8126aa861a15bbe31155317b1af72ba
MD5 ed257878492880f6b5bde162635c2355
BLAKE2b-256 b550ec45ce41b5453a4421e86ef5fe8d91b54ae7bf86b81c8bc620dbb178368f

See more details on using hashes here.

Supported by

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