Tasks¶
Task definition¶
The task
decorator¶
- task(*, priority=0, queue_name='default', backend='default', takes_context=False)[source]¶
The
@task
decorator defines aTask
instance. This has the following optional arguments:priority
: Sets thepriority
of theTask
. Defaults to 0.queue_name
: Sets thequeue_name
of theTask
. Defaults to"default"
.backend
: Sets thebackend
of theTask
. Defaults to"default"
.takes_context
: Controls whether theTask
function accepts aTaskContext
. Defaults toFalse
. See Task context for details.
If the defined
Task
is not valid according to the backend,InvalidTask
is raised.See defining tasks for usage examples.
Task
¶
- class Task[source]¶
Represents a Task to be run in the background. Tasks should be defined using the
task()
decorator.Attributes of
Task
cannot be modified. See modifying Tasks for details.- priority¶
The priority of the
Task
. Priorities must be between -100 and 100, where larger numbers are higher priority, and will be run sooner.The backend must have
supports_priority
set toTrue
to use this feature.
- backend¶
The alias of the backend the
Task
should be enqueued to. This must match a backend defined inBACKEND
.
- queue_name¶
The name of the queue the
Task
will be enqueued on to. Defaults to"default"
. This must match a queue defined inQUEUES
, unlessQUEUES
is set to[]
.
- run_after¶
The earliest time the
Task
will be executed. This can be atimedelta
, which is used relative to the current time, a timezone-awaredatetime
, orNone
if not constrained. Defaults toNone
.The backend must have
supports_defer
set toTrue
to use this feature. Otherwise,InvalidTask
is raised.
- using(*, priority=None, backend=None, queue_name=None, run_after=None)[source]¶
Creates a new
Task
with modified defaults. The existingTask
is left unchanged.using
allows modifying the following attributes:See modifying Tasks for usage examples.
- enqueue(*args, **kwargs)[source]¶
Enqueues the
Task
to theTask
backend for later execution.Arguments are passed to the
Task
’s function after a round-trip through ajson.dumps()
/json.loads()
cycle. Hence, all arguments must be JSON-serializable and preserve their type after the round-trip.If the
Task
is not valid according to the backend,InvalidTask
is raised.See enqueueing Tasks for usage examples.
- get_result(result_id)[source]¶
Retrieves a result by its id.
If the result does not exist,
TaskResultDoesNotExist
is raised. If the result is not the same type as the current Task,TaskResultMismatch
is raised. If the backend does not supportget_result()
,NotImplementedError
is raised.
- aget_result(*args, **kwargs)¶
The
async
variant ofget_result
.
Task context¶
- class TaskContext[source]¶
Contains context for the running
Task
. Context only passed to aTask
if it was defined withtakes_context=True
.Attributes of
TaskContext
cannot be modified.- task_result¶
The
TaskResult
currently being run.
Task results¶
- class TaskResultStatus[source]¶
An Enum representing the status of a
TaskResult
.
- class TaskResult[source]¶
The
TaskResult
stores the information about a specific execution of aTask
.Attributes of
TaskResult
cannot be modified.- id¶
A unique identifier for the result, which can be passed to
Task.get_result()
.The format of the id will depend on the backend being used. Task result ids are always strings less than 64 characters.
See Task results for more details.
- enqueued_at¶
The time when the
Task
was enqueued.
- started_at¶
The time when the
Task
began execution, on its first attempt.
- last_attempted_at¶
The time when the most recent
Task
run began execution.
- finished_at¶
The time when the
Task
finished execution, whether it failed or succeeded.
- backend¶
The backend the result is from.
- return_value[source]¶
The return value from the
Task
function.If the
Task
did not finish successfully,ValueError
is raised.See return values for usage examples.
- arefresh()¶
The
async
variant ofTaskResult.refresh()
.
- attempts[source]¶
The number of times the Task has been run.
If the task is currently running, it does not count as an attempt.
- worker_ids¶
The ids of the workers which have executed the Task.
Task errors¶
Backends¶
Base backend¶
- class BaseTaskBackend[source]¶
BaseTaskBackend
is the parent class for all Task backends.- options¶
A dictionary of extra parameters for the Task backend. These are provided using the
OPTIONS
setting.
- enqueue(task, args, kwargs)[source]¶
Task backends which subclass
BaseTaskBackend
should implement this method as a minimum.When implemented,
enqueue()
enqueues thetask
, aTask
instance, for later execution.args
are the positional arguments andkwargs
are the keyword arguments to be passed to thetask
. Returns aTaskResult
.
- aenqueue(task, args, kwargs)¶
The
async
variant ofBaseTaskBackend.enqueue()
.
- get_result(result_id)[source]¶
Retrieve a result by its id. If the result does not exist,
TaskResultDoesNotExist
is raised.If the backend does not support
get_result()
,NotImplementedError
is raised.
- aget_result(result_id)¶
The
async
variant ofBaseTaskBackend.get_result()
.
- validate_task(task)[source]¶
Validates whether the provided
Task
is able to be enqueued using the backend. If the Task is not valid,InvalidTask
is raised.
Feature flags¶
Some backends may not support all features Django provides. It’s possible to identify the supported functionality of a backend, and potentially change behavior accordingly.
- BaseTaskBackend.supports_defer¶
Whether the backend supports enqueueing Tasks to be executed after a specific time using the
run_after
attribute.
- BaseTaskBackend.supports_async_task¶
Whether the backend supports enqueueing async functions (coroutines).
- BaseTaskBackend.supports_get_result¶
Whether the backend supports retrieving
Task
results from another thread after they have been enqueued.
- BaseTaskBackend.supports_priority¶
Whether the backend supports executing Tasks as ordered by their
priority
.
The below table notes which of the built-in backends support which features:
Feature |
||
---|---|---|
Yes |
No |
|
Yes |
Yes |
|
No |
No [1] |
|
Yes [2] |
Yes [3] |
Available backends¶
Immediate backend¶
- class ImmediateBackend[source]¶
The immediate backend executes Tasks immediately, rather than in the background.
Dummy backend¶
Exceptions¶
- exception InvalidTaskBackend[source]¶
Raised when the requested
BaseTaskBackend
is invalid.
- exception TaskResultDoesNotExist[source]¶
Raised by
get_result()
when the providedresult_id
does not exist.
- exception TaskResultMismatch[source]¶
Raised by
get_result()
when the providedresult_id
is for a different Task than the current Task.
Footnotes