Hint: Default running tasks show status 'PENDING' and result None [OK]
Common Mistakes:
Mistaking for 'RUNNING' status (doesn't exist)
Confusing with 'STARTED' which requires explicit update_state
Thinking result is available before completion
4. You wrote:
result = AsyncResult(task_id)
if result.status == 'SUCCESS':
print(result.result)
else:
print('Task not done')
But it always prints 'Task not done' even after task completion. What is the likely issue?
medium
A. You must call result.get() instead of accessing result.result
B. You should check for 'COMPLETED' instead of 'SUCCESS'
C. The task ID is incorrect or expired
D. AsyncResult does not have a status attribute
Solution
Step 1: Understand status checking logic
The code checks if result.status equals 'SUCCESS' to print the result.
Step 2: Identify why status never shows 'SUCCESS'
If the task ID is wrong or expired, AsyncResult will not find the task and status stays 'PENDING' or similar.
Final Answer:
The task ID is incorrect or expired -> Option C
Quick Check:
Wrong task ID causes status never to be 'SUCCESS' [OK]
Hint: Check task ID validity if status never changes [OK]
Common Mistakes:
Using wrong status string like 'COMPLETED'
Assuming result.result always updates without completion
Ignoring task ID correctness
5. You want to handle a task result in Django only if it succeeded, otherwise log the error. Which code snippet correctly checks the task status and safely accesses the result or error?
hard
A. result = AsyncResult(task_id)
if result.ready():
handle(result.result)
else:
log_error('Task not ready')
B. result = AsyncResult(task_id)
try:
handle(result.get(timeout=1))
except Exception as e:
log_error(e)
C. result = AsyncResult(task_id)
if result.status == 'PENDING':
handle(result.result)
else:
log_error('Task failed')
D. result = AsyncResult(task_id)
if result.status == 'SUCCESS':
handle(result.result)
elif result.status == 'FAILURE':
log_error(result.get())
Solution
Step 1: Understand safe result retrieval
Using result.get() with a timeout waits for completion and raises exceptions on failure.
Step 2: Check error handling approach
Wrapping result.get() in try-except catches task failures and allows logging errors safely.
Step 3: Compare other options
Options B, C, and D do not handle exceptions properly; C incorrectly treats 'PENDING' as success.
Final Answer:
Use try-except with result.get() to handle success and failure -> Option B
Quick Check:
Use result.get() with try-except for safe task result handling [OK]
Hint: Use try-except with result.get() to catch errors [OK]
Common Mistakes:
Checking only status strings without exception handling