0
0
Djangoframework~20 mins

When async helps and when it does not in Django - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Async Django Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate
2:00remaining
When does async improve Django view performance?
Which scenario below best explains when using async views in Django will improve performance?
AWhen the view waits for slow external APIs or database queries and can release the thread during waiting.
BWhen the view performs long CPU-heavy calculations without waiting for external resources.
CWhen the view only renders a simple template with no external calls.
DWhen the view uses synchronous ORM queries exclusively.
Attempts:
2 left
💡 Hint

Think about when releasing the thread during waiting helps.

component_behavior
intermediate
2:00remaining
Output of async Django view with blocking code
What will be the behavior of this async Django view when called multiple times concurrently? ```python from django.http import JsonResponse import time async def my_view(request): time.sleep(2) # blocking call return JsonResponse({'status': 'done'}) ```
Django
from django.http import JsonResponse
import time

async def my_view(request):
    time.sleep(2)  # blocking call
    return JsonResponse({'status': 'done'})
AAll requests will be handled concurrently without delay.
BThe view will return immediately without waiting.
CThe server will raise a SyntaxError due to async with time.sleep.
DEach request will block the server thread for 2 seconds, causing sequential delays.
Attempts:
2 left
💡 Hint

Consider what happens when blocking code runs inside async functions.

📝 Syntax
advanced
2:00remaining
Identify the syntax error in async Django ORM usage
Which option contains the correct syntax for using Django's async ORM method inside an async view? ```python async def get_user(request): user = await User.objects.get(id=1) return JsonResponse({'username': user.username}) ```
Django
async def get_user(request):
    user = await User.objects.get(id=1)
    return JsonResponse({'username': user.username})
ACannot use await with ORM; must call get() without await in async views.
BCorrect as is, Django ORM supports await on get() in async views.
CMust use sync_to_async wrapper around User.objects.get() to await it.
DMust define get_user as a synchronous function to use ORM.
Attempts:
2 left
💡 Hint

Check if Django ORM methods are natively async or require wrappers.

state_output
advanced
2:00remaining
Effect of async on Django middleware execution order
Given this middleware setup, what is the order of print statements when a request is processed? ```python class SyncMiddleware: def __init__(self, get_response): self.get_response = get_response print('SyncMiddleware init') def __call__(self, request): print('SyncMiddleware before') response = self.get_response(request) print('SyncMiddleware after') return response class AsyncMiddleware: def __init__(self, get_response): self.get_response = get_response print('AsyncMiddleware init') async def __call__(self, request): print('AsyncMiddleware before') response = await self.get_response(request) print('AsyncMiddleware after') return response ``` Middleware order in settings: [SyncMiddleware, AsyncMiddleware]
Django
class SyncMiddleware:
    def __init__(self, get_response):
        self.get_response = get_response
        print('SyncMiddleware init')

    def __call__(self, request):
        print('SyncMiddleware before')
        response = self.get_response(request)
        print('SyncMiddleware after')
        return response

class AsyncMiddleware:
    def __init__(self, get_response):
        self.get_response = get_response
        print('AsyncMiddleware init')

    async def __call__(self, request):
        print('AsyncMiddleware before')
        response = await self.get_response(request)
        print('AsyncMiddleware after')
        return response
AAsyncMiddleware init, SyncMiddleware init, AsyncMiddleware before, SyncMiddleware before, SyncMiddleware after, AsyncMiddleware after
BSyncMiddleware init, AsyncMiddleware init, SyncMiddleware before, AsyncMiddleware before, AsyncMiddleware after, SyncMiddleware after
CSyncMiddleware init, AsyncMiddleware init, AsyncMiddleware before, SyncMiddleware before, SyncMiddleware after, AsyncMiddleware after
DAsyncMiddleware init, SyncMiddleware init, SyncMiddleware before, AsyncMiddleware before, AsyncMiddleware after, SyncMiddleware after
Attempts:
2 left
💡 Hint

Remember middleware __init__ runs on startup, __call__ runs per request in order.

🔧 Debug
expert
2:00remaining
Debugging async Django view causing server hang
You wrote this async Django view but the server hangs and never responds: ```python from django.http import JsonResponse import asyncio async def slow_view(request): asyncio.sleep(3) return JsonResponse({'status': 'done'}) ``` What is the cause of the hang?
Django
from django.http import JsonResponse
import asyncio

async def slow_view(request):
    asyncio.sleep(3)
    return JsonResponse({'status': 'done'})
Aasyncio.sleep is a coroutine and must be awaited; missing await causes the hang.
Basyncio.sleep is blocking and freezes the event loop.
CThe view must be synchronous to use sleep.
DJsonResponse cannot be returned from async views.
Attempts:
2 left
💡 Hint

Check how to properly use async sleep functions.