Skip to main content

TaskResult

This class represents the outcome of a task execution, providing detailed metadata such as status, timing information, and worker history. It allows users to access the task's return value or errors and includes methods to synchronize the local state with the backend task store. The class also tracks execution attempts and provides properties to check if the task has reached a terminal state.

Attributes

AttributeTypeDescription
task[Task](task.md?sid=django_tasks_base_task)The Task instance associated with this result.
idstrUnique identifier for the task result.
status[TaskResultStatus](taskresultstatus.md?sid=django_tasks_base_taskresultstatus)The current execution state of the task.
enqueued_at`datetimeNone`
started_at`datetimeNone`
finished_at`datetimeNone`
last_attempted_at`datetimeNone`
argslist[Any]Arguments to pass to the task function.
kwargsdict[str, Any]Keyword arguments to pass to the task function.
backendstrThe name of the storage backend used to persist the task result.
errorslist[[TaskError](taskerror.md?sid=django_tasks_base_taskerror)]Errors raised when running the task.
worker_idslist[str]Workers which have processed the task.

Constructor

Signature

def TaskResult(
task: [Task](task.md?sid=django_tasks_base_task),
id: str,
status: [TaskResultStatus](taskresultstatus.md?sid=django_tasks_base_taskresultstatus),
enqueued_at: datetime | None,
started_at: datetime | None,
finished_at: datetime | None,
last_attempted_at: datetime | None,
args: list[Any],
kwargs: dict[str, Any],
backend: str,
errors: list[[TaskError](taskerror.md?sid=django_tasks_base_taskerror)],
worker_ids: list[str]
) - > null

Parameters

NameTypeDescription
task[Task](task.md?sid=django_tasks_base_task)The task associated with this result.
idstrUnique identifier for the task result.
status[TaskResultStatus](taskresultstatus.md?sid=django_tasks_base_taskresultstatus)The current status of the task execution.
enqueued_at`datetimeNone`
started_at`datetimeNone`
finished_at`datetimeNone`
last_attempted_at`datetimeNone`
argslist[Any]Arguments passed to the task function.
kwargsdict[str, Any]Keyword arguments passed to the task function.
backendstrThe backend used for the task.
errorslist[[TaskError](taskerror.md?sid=django_tasks_base_taskerror)]Errors raised during task execution.
worker_idslist[str]List of workers that processed the task.

Methods


return_value()

@classmethod
def return_value() - > Any

The return value of the task. If the task didn't succeed, an exception is raised. This is to distinguish against the task returning None.

Returns

TypeDescription
AnyThe value returned by the executed task function if the status is SUCCESSFUL.

is_finished()

@classmethod
def is_finished() - > boolean

Checks if the task has reached a terminal state, either by succeeding or failing.

Returns

TypeDescription
booleanTrue if the task status is FAILED or SUCCESSFUL, otherwise False.

attempts()

@classmethod
def attempts() - > integer

Calculates the total number of times the task has been processed based on the number of worker IDs associated with it.

Returns

TypeDescription
integerThe count of workers that have attempted to process this task.

refresh()

@classmethod
def refresh() - > null

Reload the cached task data from the task store.

Returns

TypeDescription
null

arefresh()

@classmethod
def arefresh() - > Coroutine

Reload the cached task data from the task store

Returns

TypeDescription
CoroutineAn awaitable that updates the instance attributes with the latest data from the backend.