Bird
0
0

Given this async middleware snippet, what will be printed when a request is processed?

medium📝 component behavior Q13 of 15
Django - Async Django
Given this async middleware snippet, what will be printed when a request is processed?
class LogMiddleware:
    def __init__(self, get_response):
        self.get_response = get_response

    async def __call__(self, request):
        print('Before response')
        response = await self.get_response(request)
        print('After response')
        return response
ANo output printed due to async
BOnly Before response printed, then returns
COnly After response printed, then returns
DBefore response printed, then After response printed after response
Step-by-Step Solution
Solution:
  1. Step 1: Analyze print statements order

    The middleware prints 'Before response' before awaiting the response, then prints 'After response' after awaiting.
  2. Step 2: Understand async call flow

    Await pauses until response is ready, so both prints happen in order around the response.
  3. Final Answer:

    Before response printed, then After response printed after response -> Option D
  4. Quick Check:

    Print before and after await = Before response printed, then After response printed after response [OK]
Quick Trick: Print before and after await shows both messages in order [OK]
Common Mistakes:
MISTAKES
  • Thinking async prevents print output
  • Assuming only one print runs
  • Confusing order of prints around await

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Django Quizzes