Bird
0
0

Consider this middleware code:

medium📝 component behavior Q4 of 15
Flask - Middleware and Extensions
Consider this middleware code:
class TimingMiddleware:
    def __init__(self, app):
        self.app = app
    def __call__(self, environ, start_response):
        print('Start')
        result = self.app(environ, start_response)
        print('End')
        return result

app.wsgi_app = TimingMiddleware(app.wsgi_app)

What will be printed when a request is processed?
AOnly Start is printed, End is never printed
BStart printed before and End printed after the app processes the request
COnly End is printed, Start is never printed
DNeither Start nor End is printed
Step-by-Step Solution
Solution:
  1. Step 1: Analyze __call__ Execution

    Print 'Start' before calling the wrapped app, then print 'End' after.
  2. Step 2: Understand Flow

    The wrapped app is called synchronously, so both prints occur in order.
  3. Final Answer:

    Start printed before and End printed after the app processes the request -> Option B
  4. Quick Check:

    Both prints surround the app call [OK]
Quick Trick: Middleware prints before and after app call [OK]
Common Mistakes:
MISTAKES
  • Assuming prints happen asynchronously
  • Thinking app call prevents 'End' print
  • Ignoring middleware call order

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Flask Quizzes