Challenge - 5 Problems
Async Middleware Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the output of this async middleware logging example?
Consider this Django async middleware that logs request and response types. What will be printed in the console when a request is processed?
Django
import asyncio from django.utils.deprecation import MiddlewareMixin class AsyncLoggingMiddleware: async def __call__(self, request): print(f"Request type: {type(request)}") response = await self.get_response(request) print(f"Response type: {type(response)}") return response
Attempts:
2 left
💡 Hint
Remember that Django passes HttpRequest objects to middleware and expects HttpResponse objects back.
✗ Incorrect
The middleware receives a HttpRequest object and awaits the response, which is a HttpResponse object. The types printed reflect these classes.
📝 Syntax
intermediate2:00remaining
Which async middleware definition is syntactically correct in Django?
Select the option that correctly defines an async middleware class in Django 4+.
Attempts:
2 left
💡 Hint
Async middleware must define an async __call__ method and await the response.
✗ Incorrect
Option B correctly defines an async __call__ method and awaits get_response. Option B is sync and does not await. Option B uses await in a sync method causing syntax error. Option B uses process_request which is not async middleware pattern.
🔧 Debug
advanced2:00remaining
Why does this async middleware cause a runtime error?
Given this middleware code, what is the cause of the runtime error when processing requests asynchronously?
class FaultyMiddleware:
def __call__(self, request):
response = self.get_response(request)
return response
Attempts:
2 left
💡 Hint
Check if async functions are awaited properly in sync methods.
✗ Incorrect
The get_response callable is async in async middleware chains. Calling it without await in a sync __call__ method returns a coroutine that is never awaited, causing runtime errors.
❓ state_output
advanced2:00remaining
What is the final value of request.state['count'] after this async middleware chain?
Two async middlewares increment a counter in request.state. What is the final count after processing a request?
Middleware1:
async def __call__(self, request):
request.state['count'] = request.state.get('count', 0) + 1
response = await self.get_response(request)
return response
Middleware2:
async def __call__(self, request):
request.state['count'] = request.state.get('count', 0) + 1
response = await self.get_response(request)
return response
Attempts:
2 left
💡 Hint
Each middleware increments the count once before passing the request on.
✗ Incorrect
Both middlewares increment the count by 1. The first sets count=1, then calls next middleware which sets count=2. So final count is 2.
🧠 Conceptual
expert2:00remaining
Which statement best describes async middleware behavior in Django 4+?
Choose the most accurate statement about async middleware in Django 4 and later versions.
Attempts:
2 left
💡 Hint
Think about how Django handles sync and async views with async middleware.
✗ Incorrect
Django's async middleware can await get_response which adapts to sync or async views internally, allowing seamless calling of both types.