In addition to the above attributes, run_after can be passed to specify a specific time the task should run.
Task context
Sometimes the running task may need to know context about how it was enqueued. To receive the task context as an argument to your task function, pass takes_context to the decorator and ensure the task takes a context as the first argument.
This API will be extended with additional features in future.
Enqueueing tasks
To execute a task, call the enqueue method on it:
result = calculate_meaning_of_life.enqueue()
The returned TaskResult can be interrogated to query the current state of the running task, as well as its return value.
If the task takes arguments, these can be passed as-is to enqueue.
Queue names
By default, tasks are enqueued onto the "default" queue. When using multiple queues, it can be useful to constrain the allowed names, so tasks aren't missed.
Enqueueing tasks to an unknown queue name raises InvalidTaskError.
To disable queue name validation, set QUEUES to [].
Retrieving task result
When enqueueing a task, you get a TaskResult, however it may be useful to retrieve said result from somewhere else (another request, another task etc). This can be done with get_result (or aget_result):
A result id should be considered an opaque string, whose length could be up to 64 characters. ID generation is backend-specific.
Only tasks of the same type can be retrieved this way. To retrieve the result of any task, you can call get_result on the backend:
from django_tasks import default_task_backend
default_task_backend.get_result(result_id)
Return values
If your task returns something, it can be retrieved from the .return_value attribute on a TaskResult. Accessing this property on an unsuccessful task (ie not SUCCESSFUL) will raise a ValueError.
If a result has been updated in the background, you can call refresh on it to update its values. Results obtained using get_result will always be up-to-date.
Note that this is just the type of exception, and contains no other values. The traceback information is reduced to a string that you can print to help debugging:
Note that currently, whilst .errors is a list, it will only ever contain a single element.
Attempts
The number of times a task has been run is stored as the .attempts attribute. This will currently only ever be 0 or 1.
The date of the last attempt is stored as .last_attempted_at.
Backend introspecting
Because django-tasks enables support for multiple different backends, those backends may not support all features, and it can be useful to determine this at runtime to ensure the chosen task queue meets the requirements, or to gracefully degrade functionality if it doesn't.
supports_defer: Can tasks be enqueued with the run_after attribute?
supports_async_task: Can coroutines be enqueued?
supports_get_result: Can results be retrieved after the fact (from any thread / process)?
supports_priority: Can tasks be executed in a given priority order?
from django_tasks import default_task_backend
assert default_task_backend.supports_get_result
A few Signals are provided to more easily respond to certain task events.
Whilst signals are available, they may not be the most maintainable approach.
django_tasks.signals.task_enqueued: Called when a task is enqueued. The sender is the backend class. Also called with the enqueued task_result.
django_tasks.signals.task_finished: Called when a task finishes (SUCCESSFUL or FAILED). The sender is the backend class. Also called with the finished task_result.
django_tasks.signals.task_started: Called immediately before a task starts executing. The sender is the backend class. Also called with the started task_result.