Flask interview question · Most asked
What is WSGI and what role does Werkzeug play in Flask?
Summary
WSGI (PEP 3333) is the standard interface between Python web servers and apps: the app is a callable app(environ, start_response). Werkzeug is the WSGI toolkit Flask uses for request/response objects, routing, the dev server, the debugger and utilities.
Longer Answer
When Gunicorn receives a request it builds the environ dict and calls app.wsgi_app. Flask pushes contexts, dispatches to the view and returns a response iterable. Because the interface is synchronous, a WSGI worker handles one request at a time (per thread/greenlet).
Code Example
def simple_wsgi_app(environ, start_response):
start_response("200 OK", [("Content-Type", "text/plain")])
return [b"Hello from WSGI"]
Key Points
app(environ, start_response)- Werkzeug: Request/Response, routing, debugger
- Sync request/response model
- Middleware wraps
wsgi_app
More most-asked Flask questions
- How do you access request data and build responses in Flask?
- How do Flask sessions work and what are their security implications?
- What is the application factory pattern in Flask and why use it?
- What is Flask and what does 'microframework' mean?
- 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 use SQLAlchemy with Flask (Flask-SQLAlchemy 3.x)?