mirror of
https://github.com/django/django.git
synced 2025-11-07 07:15:35 +00:00
Fixed #35859 -- Added background Tasks framework interface.
This work implements what was defined in DEP 14 (https://github.com/django/deps/blob/main/accepted/0014-background-workers.rst). Thanks to Raphael Gaschignard, Eric Holscher, Ran Benita, Sarah Boyce, Jacob Walls, and Natalia Bidart for the reviews.
This commit is contained in:
@@ -112,6 +112,45 @@ A `migration guide`_ is available if you're updating from the
|
||||
|
||||
.. _migration guide: https://github.com/carltongibson/django-template-partials/blob/main/Migration.md
|
||||
|
||||
Background Tasks
|
||||
----------------
|
||||
|
||||
Django now includes a built-in Tasks framework for running code outside the
|
||||
HTTP request–response cycle. This enables offloading work, such as sending
|
||||
emails or processing data, to background workers.
|
||||
|
||||
Tasks are defined using the :func:`~django.tasks.task` decorator::
|
||||
|
||||
from django.core.mail import send_mail
|
||||
from django.tasks import task
|
||||
|
||||
|
||||
@task
|
||||
def email_users(emails, subject, message):
|
||||
return send_mail(subject, message, None, emails)
|
||||
|
||||
Once defined, tasks can be enqueued through a configured backend::
|
||||
|
||||
email_users.enqueue(
|
||||
emails=["user@example.com"],
|
||||
subject="You have a message",
|
||||
message="Hello there!",
|
||||
)
|
||||
|
||||
Backends are configured via the :setting:`TASKS` setting. Django provides
|
||||
two built-in backends, primarily for development and testing:
|
||||
|
||||
* :class:`~django.tasks.backends.immediate.ImmediateBackend`: executes tasks
|
||||
immediately in the same process.
|
||||
* :class:`~django.tasks.backends.dummy.DummyBackend`: stores tasks without
|
||||
running them, leaving results in the
|
||||
:attr:`~django.tasks.TaskResultStatus.READY` state.
|
||||
|
||||
Django only handles task creation and queuing; it does not provide a worker
|
||||
mechanism to run tasks. Execution must be managed by external infrastructure,
|
||||
such as a separate process or service. See :doc:`/topics/tasks` for an
|
||||
overview, and the :doc:`Tasks reference </ref/tasks>` for API details.
|
||||
|
||||
Minor features
|
||||
--------------
|
||||
|
||||
|
||||
Reference in New Issue
Block a user