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

← All Flask questions