A reference implementation and backport of background workers and tasks in Django
Project description
Django Tasks
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
python -m 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 add django_tasks to your INSTALLED_APPS.
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 threaddjango_tasks.backends.database.DatabaseBackend: Store tasks in the database (via Django's ORM), and retrieve and execute them using thedb_workermanagement command
Note: DatabaseBackend additionally requires django_tasks.backends.database adding to INSTALLED_APPS.
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 queuebackend: Name of the backend for this task to use (as defined inTASKS)
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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file django_tasks-0.2.0.tar.gz.
File metadata
- Download URL: django_tasks-0.2.0.tar.gz
- Upload date:
- Size: 18.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.0 CPython/3.12.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
638c543327810735228b99a1f43ed1f16a56551f9edd01be50fac17982926c5b
|
|
| MD5 |
aa44c139af48764e30fa3f2369b0333a
|
|
| BLAKE2b-256 |
563b73f94425da75fa35c4bcb7a71fab5a68ef64a9e4ced1c39b60d395615b42
|
File details
Details for the file django_tasks-0.2.0-py3-none-any.whl.
File metadata
- Download URL: django_tasks-0.2.0-py3-none-any.whl
- Upload date:
- Size: 22.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/5.1.0 CPython/3.12.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a39c6e932cb2e45dfbc63a4bb701d0955ca533fb3eecf9a68e20707446dd808c
|
|
| MD5 |
c4457d1039eb499f76ab25c3a857984c
|
|
| BLAKE2b-256 |
4fcf4b040b828583078b769c327d0b494e7ac73dedd272fd1e71fa577767f271
|