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

← All Flask questions