TasksLink para este cabeçalho
The Task framework provides the contract and plumbing for background work, not the engine that runs it. The Tasks API defines how work is described, queued, and tracked, but leaves actual execution to external infrastructure.
Task definitionLink para este cabeçalho
The task decoratorLink para este cabeçalho
- task(*, priority=0, queue_name='default', backend='default', takes_context=False, **kwargs)Link para esta definição
The
@taskdecorator defines aTaskinstance. All keyword arguments are passed directly to the backend’stask_class(which defaults toTask).The following standard arguments are supported:
priority: Sets thepriorityof theTask. Defaults to 0.queue_name: Sets thequeue_nameof theTask. Defaults to"default".backend: Sets thebackendof theTask. Defaults to"default".takes_context: Controls whether theTaskfunction accepts aTaskContext. Defaults toFalse. See Task context for details.
Custom Task backends may define a custom
task_classthat accepts additional arguments. These can be passed through the@taskdecorator:@task(foo=5, bar=600) def my_task(): passIf the defined
Taskis not valid according to the backend,InvalidTaskis raised.See defining tasks for usage examples.
TaskLink para este cabeçalho
- class TaskLink para esta definição
Represents a Task to be run in the background. Tasks should be defined using the
task()decorator.Attributes of
Taskcannot be modified. See modifying Tasks for details.- priorityLink para esta definição
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_priorityset toTrueto use this feature.
- backendLink para esta definição
The alias of the backend the
Taskshould be enqueued to. This must match a backend defined inBACKEND.
- queue_nameLink para esta definição
The name of the queue the
Taskwill be enqueued on to. Defaults to"default". This must match a queue defined inQUEUES, unlessQUEUESis set to[].
- run_afterLink para esta definição
The earliest time the
Taskwill be executed. This can be atimedelta, which is used relative to the current time, a timezone-awaredatetime, orNoneif not constrained. Defaults toNone.This attribute can be set using
using().The backend must have
supports_deferset toTrueto use this feature. Otherwise,InvalidTaskis raised.
- nameLink para esta definição
The name of the function decorated with
task(). This name is not necessarily unique.
- using(*, priority=None, backend=None, queue_name=None, run_after=None)Link para esta definição
Creates a new
Taskwith modified defaults. The existingTaskis left unchanged.usingallows modifying the following attributes:See modifying Tasks for usage examples.
- enqueue(*args, **kwargs)Link para esta definição
Enqueues the
Taskto theTaskbackend 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
Taskis not valid according to the backend,InvalidTaskis raised.See enqueueing Tasks for usage examples.
- aenqueue(*args, **kwargs)Link para esta definição
The
asyncvariant ofenqueue.
- get_result(result_id)Link para esta definição
Retrieves a result by its id.
If the result does not exist,
TaskResultDoesNotExistis raised. If the result is not the same type as the current Task,TaskResultMismatchis raised. If the backend does not supportget_result(),NotImplementedErroris raised.
- aget_result(*args, **kwargs)Link para esta definição
The
asyncvariant ofget_result.
Task contextLink para este cabeçalho
- class TaskContextLink para esta definição
Contains context for the running
Task. Context only passed to aTaskif it was defined withtakes_context=True.Attributes of
TaskContextcannot be modified.- task_resultLink para esta definição
The
TaskResultcurrently being run.
- attemptLink para esta definição
The number of the current execution attempts for this Task, starting at 1.
Task resultsLink para este cabeçalho
- class TaskResultStatusLink para esta definição
An Enum representing the status of a
TaskResult.- READYLink para esta definição
The
Taskhas just been enqueued, or is ready to be executed again.
- RUNNINGLink para esta definição
The
Taskis currently being executed.
- FAILEDLink para esta definição
The
Taskraised an exception during execution, or was unable to start.
- SUCCESSFULLink para esta definição
The
Taskhas finished executing successfully.
- class TaskResultLink para esta definição
The
TaskResultstores the information about a specific execution of aTask.Attributes of
TaskResultcannot be modified.- taskLink para esta definição
The
Taskthe result was enqueued for.
- idLink para esta definição
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.
- statusLink para esta definição
The
statusof the result.
- enqueued_atLink para esta definição
The time when the
Taskwas enqueued.
- started_atLink para esta definição
The time when the
Taskbegan execution, on its first attempt.
- last_attempted_atLink para esta definição
The time when the most recent
Taskrun began execution.
- finished_atLink para esta definição
The time when the
Taskfinished execution, whether it failed or succeeded.
- backendLink para esta definição
The backend the result is from.
- errorsLink para esta definição
A list of
TaskErrorinstances for the errors raised as part of each execution of the Task.
- return_valueLink para esta definição
The return value from the
Taskfunction.If the
Taskdid not finish successfully,ValueErroris raised.See return values for usage examples.
- refresh()Link para esta definição
Refresh the result’s attributes from the queue store.
- arefresh()Link para esta definição
The
asyncvariant ofTaskResult.refresh().
- is_finishedLink para esta definição
Whether the
Taskhas finished (successfully or not).
- attemptsLink para esta definição
The number of times the Task has been run.
If the task is currently running, it does not count as an attempt.
- worker_idsLink para esta definição
The ids of the workers which have executed the Task.
Task errorsLink para este cabeçalho
- class TaskErrorLink para esta definição
Contains information about the error raised during the execution of a
Task.- tracebackLink para esta definição
The traceback (as a string) from the raised exception when the
Taskfailed.
- exception_classLink para esta definição
The exception class raised when executing the
Task.
BackendsLink para este cabeçalho
Backends handle how Tasks are stored and executed. All backends share a common
interface defined by BaseTaskBackend, which specifies the core methods for
enqueueing Tasks and retrieving results.
Base backendLink para este cabeçalho
- class BaseTaskBackendLink para esta definição
BaseTaskBackendis the parent class for all Task backends.- task_classLink para esta definição
The
Tasksubclass to use when creating tasks with thetask()decorator. Defaults toTask. Custom backends can override this to use a customTasksubclass with additional attributes.
- optionsLink para esta definição
A dictionary of extra parameters for the Task backend. These are provided using the
OPTIONSsetting.
- enqueue(task, args, kwargs)Link para esta definição
Task backends which subclass
BaseTaskBackendshould implement this method as a minimum.When implemented,
enqueue()enqueues thetask, aTaskinstance, for later execution.argsare the positional arguments andkwargsare the keyword arguments to be passed to thetask. Returns aTaskResult.
- aenqueue(task, args, kwargs)Link para esta definição
The
asyncvariant ofBaseTaskBackend.enqueue().
- get_result(result_id)Link para esta definição
Retrieve a result by its id. If the result does not exist,
TaskResultDoesNotExistis raised.If the backend does not support
get_result(),NotImplementedErroris raised.
- aget_result(result_id)Link para esta definição
The
asyncvariant ofBaseTaskBackend.get_result().
- validate_task(task)Link para esta definição
Validates whether the provided
Taskis able to be enqueued using the backend. If the Task is not valid,InvalidTaskis raised.
Feature flagsLink para este cabeçalho
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_deferLink para esta definição
Whether the backend supports enqueueing Tasks to be executed after a specific time using the
run_afterattribute.
- BaseTaskBackend.supports_async_taskLink para esta definição
Whether the backend supports enqueueing async functions (coroutines).
- BaseTaskBackend.supports_get_resultLink para esta definição
Whether the backend supports retrieving
Taskresults from another thread after they have been enqueued.
- BaseTaskBackend.supports_priorityLink para esta definição
Whether the backend supports executing Tasks as ordered by their
priority.
The below table notes which of the built-in backends support which features:
Available backendsLink para este cabeçalho
Django includes only development and testing backends. These support local execution and inspection, for production ready backends refer to Configuring a Task backend.
Immediate backendLink para este cabeçalho
- class ImmediateBackendLink para esta definição
The immediate backend executes Tasks immediately, rather than in the background.
Dummy backendLink para este cabeçalho
- class DummyBackendLink para esta definição
The dummy backend does not execute enqueued Tasks. Instead, it stores task results for later inspection.
- resultsLink para esta definição
A list of results for the enqueued Tasks, in the order they were enqueued.
- clear()Link para esta definição
Clears the list of stored results.
ExceçõesLink para este cabeçalho
- exception InvalidTaskLink para esta definição
Raised when the
Taskattempting to be enqueued is invalid.
- exception InvalidTaskBackendLink para esta definição
Raised when the requested
BaseTaskBackendis invalid.
- exception TaskResultDoesNotExistLink para esta definição
Raised by
get_result()when the providedresult_iddoes not exist.
- exception TaskResultMismatchLink para esta definição
Raised by
get_result()when the providedresult_idis for a different Task than the current Task.
Notas de rodapé