{"id":18180571,"url":"https://github.com/realOrangeOne/django-tasks","last_synced_at":"2025-04-01T20:30:53.023Z","repository":{"id":221718566,"uuid":"755166719","full_name":"RealOrangeOne/django-tasks","owner":"RealOrangeOne","description":"A reference implementation and backport of background workers and tasks in Django","archived":false,"fork":false,"pushed_at":"2025-02-16T18:21:50.000Z","size":341,"stargazers_count":538,"open_issues_count":15,"forks_count":33,"subscribers_count":20,"default_branch":"master","last_synced_at":"2025-03-31T02:00:39.769Z","etag":null,"topics":["background-workers","django"],"latest_commit_sha":null,"homepage":"https://pypi.org/project/django-tasks/","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/RealOrangeOne.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":"RealOrangeOne","ko_fi":"theorangeone","liberapay":"theorangeone","custom":["https://theorangeone.net/support/"]}},"created_at":"2024-02-09T15:06:18.000Z","updated_at":"2025-03-30T23:01:47.000Z","dependencies_parsed_at":"2024-04-30T12:11:12.470Z","dependency_job_id":"57b8e74a-e859-4cb2-98f5-b06a6a8fd3a1","html_url":"https://github.com/RealOrangeOne/django-tasks","commit_stats":{"total_commits":172,"total_committers":15,"mean_commits":"11.466666666666667","dds":0.313953488372093,"last_synced_commit":"e6d26ca2a233e86681b87c357d30af39f648dd9c"},"previous_names":["realorangeone/django-core-tasks","realorangeone/django-tasks"],"tags_count":9,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RealOrangeOne%2Fdjango-tasks","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RealOrangeOne%2Fdjango-tasks/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RealOrangeOne%2Fdjango-tasks/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/RealOrangeOne%2Fdjango-tasks/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/RealOrangeOne","download_url":"https://codeload.github.com/RealOrangeOne/django-tasks/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246709923,"owners_count":20821297,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["background-workers","django"],"created_at":"2024-11-02T19:00:54.684Z","updated_at":"2025-04-01T20:30:52.996Z","avatar_url":"https://github.com/RealOrangeOne.png","language":"Python","funding_links":["https://github.com/sponsors/RealOrangeOne","https://ko-fi.com/theorangeone","https://liberapay.com/theorangeone","https://theorangeone.net/support/"],"categories":["Third-Party Packages"],"sub_categories":["Task Queues"],"readme":"# Django Tasks\n\n[![CI](https://github.com/RealOrangeOne/django-tasks/actions/workflows/ci.yml/badge.svg)](https://github.com/RealOrangeOne/django-tasks/actions/workflows/ci.yml)\n![PyPI](https://img.shields.io/pypi/v/django-tasks.svg)\n![PyPI - Python Version](https://img.shields.io/pypi/pyversions/django-tasks.svg)\n![PyPI - Status](https://img.shields.io/pypi/status/django-tasks.svg)\n![PyPI - License](https://img.shields.io/pypi/l/django-tasks.svg)\n\nAn implementation and backport of background workers and tasks in Django, as defined in [DEP 0014](https://github.com/django/deps/blob/main/accepted/0014-background-workers.rst).\n\n**Warning**: This package is under active development, and breaking changes may be released at any time. Be sure to pin to specific versions if you're using this package in a production environment.\n\n## Installation\n\n```\npython -m pip install django-tasks\n```\n\nThe first step is to add `django_tasks` to your `INSTALLED_APPS`.\n\n```python\nINSTALLED_APPS = [\n    # ...\n    \"django_tasks\",\n]\n```\n\nSecondly, you'll need to configure a backend. This connects the tasks to whatever is going to execute them.\n\nIf omitted, the following configuration is used:\n\n```python\nTASKS = {\n    \"default\": {\n        \"BACKEND\": \"django_tasks.backends.immediate.ImmediateBackend\"\n    }\n}\n```\n\nA few backends are included by default:\n\n- `django_tasks.backends.dummy.DummyBackend`: Don't execute the tasks, just store them. This is especially useful for testing.\n- `django_tasks.backends.immediate.ImmediateBackend`: Execute the task immediately in the current thread\n- `django_tasks.backends.database.DatabaseBackend`: Store tasks in the database (via Django's ORM), and retrieve and execute them using the `db_worker` management command\n\nNote: `DatabaseBackend` additionally requires `django_tasks.backends.database` adding to `INSTALLED_APPS`.\n\n## Usage\n\n**Note**: This documentation is still work-in-progress. Further details can also be found on the [DEP](https://github.com/django/deps/blob/main/accepted/0014-background-workers.rst). [The tests](./tests/tests/) are also a good exhaustive reference.\n\n### Defining tasks\n\nA task is created with the `task` decorator.\n\n```python\nfrom django_tasks import task\n\n\n@task()\ndef calculate_meaning_of_life() -\u003e int:\n    return 42\n```\n\nThe task decorator accepts a few arguments to customize the task:\n\n- `priority`: The priority of the task (between -100 and 100. Larger numbers are higher priority. 0 by default)\n- `queue_name`: Whether to run the task on a specific queue\n- `backend`: Name of the backend for this task to use (as defined in `TASKS`)\n- `enqueue_on_commit`: Whether the task is enqueued when the current transaction commits successfully, or enqueued immediately. By default, this is handled by the backend (see below). `enqueue_on_commit` may not be modified with `.using`.\n\nThese attributes (besides `enqueue_on_commit`) can also be modified at run-time with `.using`:\n\n```python\nmodified_task = calculate_meaning_of_life.using(priority=10)\n```\n\nIn addition to the above attributes, `run_after` can be passed to specify a specific time the task should run.\n\n### Enqueueing tasks\n\nTo execute a task, call the `enqueue` method on it:\n\n```python\nresult = calculate_meaning_of_life.enqueue()\n```\n\nThe returned `TaskResult` can be interrogated to query the current state of the running task, as well as its return value.\n\nIf the task takes arguments, these can be passed as-is to `enqueue`.\n\n#### Transactions\n\nBy default, tasks are enqueued after the current transaction (if there is one) commits successfully (using Django's `transaction.on_commit` method), rather than enqueueing immediately.\n\nThis can be configured using the `ENQUEUE_ON_COMMIT` setting. `True` and `False` force the behaviour.\n\n```python\nTASKS = {\n    \"default\": {\n        \"BACKEND\": \"django_tasks.backends.immediate.ImmediateBackend\",\n        \"ENQUEUE_ON_COMMIT\": False\n    }\n}\n```\n\nThis can also be configured per-task by passing `enqueue_on_commit` to the `task` decorator.\n\n### Queue names\n\nBy 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.\n\n```python\nTASKS = {\n    \"default\": {\n        \"BACKEND\": \"django_tasks.backends.immediate.ImmediateBackend\",\n        \"QUEUES\": [\"default\", \"special\"]\n    }\n}\n```\n\nEnqueueing tasks to an unknown queue name raises `InvalidTaskError`.\n\nTo disable queue name validation, set `QUEUES` to `[]`.\n\n### The database backend worker\n\nFirst, you'll need to add `django_tasks.backends.database`  to `INSTALLED_APPS`:\n\n```python\nINSTALLED_APPS = [\n    # ...\n    \"django_tasks\",\n    \"django_tasks.backends.database\",\n]\n```\n\nThen, run migrations:\n\n```shell\n./manage.py migrate\n```\n\nNext, configure the database backend:\n\n```python\nTASKS = {\n    \"default\": {\n        \"BACKEND\": \"django_tasks.backends.database.DatabaseBackend\"\n    }\n}\n```\n\nFinally, you can run the `db_worker` command to run tasks as they're created. Check the `--help` for more options.\n\n```shell\n./manage.py db_worker\n```\n\n### Pruning old tasks\n\nAfter a while, tasks may start to build up in your database. This can be managed using the `prune_db_task_results` management command, which deletes completed tasks according to the given retention policy. Check the `--help` for the available options.\n\n### Retrieving task result\n\nWhen 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`):\n\n```python\nresult_id = result.id\n\n# Later, somewhere else...\ncalculate_meaning_of_life.get_result(result_id)\n```\n\nA result `id` should be considered an opaque string, whose length could be up to 64 characters. ID generation is backend-specific.\n\nOnly 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:\n\n```python\nfrom django_tasks import default_task_backend\n\ndefault_task_backend.get_result(result_id)\n```\n\n### Return values\n\nIf 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 `SUCCEEDED`) will raise a `ValueError`.\n\n```python\nassert result.status == ResultStatus.SUCCEEDED\nassert result.return_value == 42\n```\n\nIf 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.\n\n```python\nassert result.status == ResultStatus.NEW\nresult.refresh()\nassert result.status == ResultStatus.SUCCEEDED\n```\n\n#### Exceptions\n\nIf a task raised an exception, its `.exception_class` will be the exception class raised:\n\n```python\nassert result.exception_class == ValueError\n```\n\nNote 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:\n\n```python\nassert isinstance(result.traceback, str)\n```\n\n### Backend introspecting\n\nBecause `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.\n\n- `supports_defer`: Can tasks be enqueued with the `run_after` attribute?\n- `supports_async_task`: Can coroutines be enqueued?\n- `supports_get_result`: Can results be retrieved after the fact (from **any** thread / process)?\n\n```python\nfrom django_tasks import default_task_backend\n\nassert default_task_backend.supports_get_result\n```\n\nThis is particularly useful in combination with Django's [system check framework](https://docs.djangoproject.com/en/stable/topics/checks/).\n\n### Signals\n\nA few [Signals](https://docs.djangoproject.com/en/stable/topics/signals/) are provided to more easily respond to certain task events.\n\nWhilst signals are available, they may not be the most maintainable approach.\n\n- `django_tasks.signals.task_enqueued`: Called when a task is enqueued. The sender is the backend class. Also called with the enqueued `task_result`.\n- `django_tasks.signals.task_finished`: Called when a task finishes (`SUCCEEDED` or `FAILED`). The sender is the backend class. Also called with the finished `task_result`.\n\n## Contributing\n\nSee [CONTRIBUTING.md](./CONTRIBUTING.md) for information on how to contribute.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FrealOrangeOne%2Fdjango-tasks","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FrealOrangeOne%2Fdjango-tasks","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FrealOrangeOne%2Fdjango-tasks/lists"}