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
| Attribute | Type | Description |
|---|---|---|
| task | [Task](task.md?sid=django_tasks_base_task) | The Task instance associated with this result. |
| id | str | Unique identifier for the task result. |
| status | [TaskResultStatus](taskresultstatus.md?sid=django_tasks_base_taskresultstatus) | The current execution state of the task. |
| enqueued_at | `datetime | None` |
| started_at | `datetime | None` |
| finished_at | `datetime | None` |
| last_attempted_at | `datetime | None` |
| args | list[Any] | Arguments to pass to the task function. |
| kwargs | dict[str, Any] | Keyword arguments to pass to the task function. |
| backend | str | The name of the storage backend used to persist the task result. |
| errors | list[[TaskError](taskerror.md?sid=django_tasks_base_taskerror)] | Errors raised when running the task. |
| worker_ids | list[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
| Name | Type | Description |
|---|---|---|
| task | [Task](task.md?sid=django_tasks_base_task) | The task associated with this result. |
| id | str | Unique identifier for the task result. |
| status | [TaskResultStatus](taskresultstatus.md?sid=django_tasks_base_taskresultstatus) | The current status of the task execution. |
| enqueued_at | `datetime | None` |
| started_at | `datetime | None` |
| finished_at | `datetime | None` |
| last_attempted_at | `datetime | None` |
| args | list[Any] | Arguments passed to the task function. |
| kwargs | dict[str, Any] | Keyword arguments passed to the task function. |
| backend | str | The backend used for the task. |
| errors | list[[TaskError](taskerror.md?sid=django_tasks_base_taskerror)] | Errors raised during task execution. |
| worker_ids | list[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
| Type | Description |
|---|---|
Any | The 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
| Type | Description |
|---|---|
boolean | True 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
| Type | Description |
|---|---|
integer | The 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
| Type | Description |
|---|---|
null |
arefresh()
@classmethod
def arefresh() - > Coroutine
Reload the cached task data from the task store
Returns
| Type | Description |
|---|---|
Coroutine | An awaitable that updates the instance attributes with the latest data from the backend. |