0
0
Djangoframework~10 mins

Task results and status in Django - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Task results and status
Start Task
Task Running
Task Completes?
NoWait or Retry
Yes
Store Result & Status
Check Status & Result
Display or Use Result
This flow shows how a task starts, runs, completes, stores its result and status, and then how the status and result are checked or displayed.
Execution Sample
Django
from django_q.tasks import async_task

# Start a task
async_task('math.copysign', 1, -1)

# Later check status and result
from django_q.models import Task
result = Task.objects.last().result
status = Task.objects.last().success
This code starts an asynchronous task and later retrieves its result and success status from the database.
Execution Table
StepActionTask StatusResult StoredNotes
1Call async_task to start taskPendingNoneTask is queued to run asynchronously
2Task starts runningRunningNoneTask is executing the function
3Task completes successfullySuccess-1.0Result of math.copysign(1, -1) stored
4Retrieve last task from DBSuccess-1.0Access task status and result
5Use result and statusSuccess-1.0Result -1.0 and success True used in app
6No new tasksN/AN/AExecution stops here
💡 No more tasks to process, final status and result stored
Variable Tracker
VariableStartAfter Step 1After Step 3After Step 4Final
task_statusNonePendingSuccessSuccessSuccess
task_resultNoneNone-1.0-1.0-1.0
Key Moments - 3 Insights
Why is the task status 'Pending' right after calling async_task?
Because the task is only queued to run asynchronously and has not started execution yet, as shown in step 1 of the execution_table.
How do we know the task completed successfully?
At step 3, the status changes to 'Success' and the result is stored, indicating the task finished without errors.
Why do we retrieve the task from the database to get the result?
Because async tasks run outside the main code flow, their results and status are saved in the database for later retrieval, as shown in step 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the task status immediately after calling async_task?
ARunning
BPending
CSuccess
DFailed
💡 Hint
Check step 1 in the execution_table where the task is just queued.
At which step does the task result get stored?
AStep 2
BStep 4
CStep 3
DStep 5
💡 Hint
Look for when the result value changes from None to -1.0 in the variable_tracker and execution_table.
If the task failed, what would you expect the 'task_status' to be at step 3?
AFailed
BRunning
CPending
DSuccess
💡 Hint
Refer to the meaning of status values in the execution_table and key_moments.
Concept Snapshot
Task results and status in Django Q:
- async_task() queues a task (status: Pending)
- Task runs asynchronously (status: Running)
- On completion, result and status saved (Success or Failed)
- Retrieve task from DB to check status and result
- Use status to handle success or failure in your app
Full Transcript
In Django Q, when you start a task with async_task, it is first queued with status Pending. Then the task runs asynchronously and its status changes to Running. When the task finishes, the result and status (Success or Failed) are saved in the database. You can later retrieve the task object from the database to check its status and get the result. This lets your app know if the task succeeded and what output it produced.