Flask interview question · Most asked
Walk through the lifecycle of a request in Flask.
Summary
The WSGI server calls app.wsgi_app; Flask creates a request context (which pushes an app context), matches the URL, runs before_request hooks, calls the view, converts the return value to a response, runs after_request hooks, then teardown functions, and pops the contexts.
Longer Answer
If a before_request hook returns a value, the view is skipped. Exceptions are routed to error handlers; teardown_* functions run even when errors occur. Signals fire at key points (request_started, request_finished).
Key Points
- Contexts pushed → URL matched
- before_request → view → after_request
- Teardown always runs
- Error handlers on exceptions
Deep dive: Request Lifecycle course: Flask: Contexts, Hooks and Teardown
More most-asked Flask questions
- What is Flask and what does 'microframework' mean?
- How do Flask sessions work and what are their security implications?
- What is the application factory pattern in Flask and why use it?
- Explain Flask's application context and request context.
- What are Blueprints in Flask?
- How does routing work in Flask, including variable rules and HTTP methods?
- How do you access request data and build responses in Flask?
- How do you use SQLAlchemy with Flask (Flask-SQLAlchemy 3.x)?