Skip to the page

Flask and choosing a framework · core

Flask contexts, the app factory and shared state

Flask gives each request an application context and a request context, held in context-local proxies. Module-level variables are shared by all threads, so per-request data must not live in them. An app factory with blueprints removes circular imports.

The notes

What you must know

  • Flask pushes an application context and a request context for each request (and an app context for CLI commands). current_app points to the app, request to the current request, and g is scratch space for the app context.
  • They are context-local proxies (Werkzeug LocalProxy over contextvars), so each request, thread or task sees its own values.
  • Outside a request (scripts, tests, setup, Celery tasks) use with app.app_context(). Tests that need request use app.test_request_context(). A new thread does not inherit the context.
  • teardown_appcontext closes per-request resources kept on g, such as a database connection.
  • App factory: create_app(config) builds the app, loads config, calls db.init_app(app), registers blueprints and returns the app. Extensions are created without an app (db = SQLAlchemy()) and bound with init_app. Views use current_app, not an imported app, which removes circular imports.
  • Secrets and SECRET_KEY must not be committed. Read them from the environment (app.config.from_prefixed_env()). debug=True on 0.0.0.0 exposes the interactive debugger, so never use it in production; run under Gunicorn behind Nginx.
  • A module-level current-user variable is shared by every thread. With a one-request-at-a-time worker it seems to work; with threads, two requests overwrite each other. Use g.user in before_request or Flask-Login current_user.
  • Search for other per-request data in globals, mutable default arguments and class attributes used as caches. Treat leaking one user's data to another as a privacy incident: restore safe settings first, then find who was affected.

The red pen

Common traps: mistake → fix

  • Mistake: A global variable is a fine place for the current user

    Fix: it is shared by every thread in the process.

  • Mistake: g lasts across requests

    Fix: it lasts for one app context, usually one request.

  • Mistake: Working outside of application context is a Flask bug

    Fix: push the context with app.app_context().

  • Mistake: from views import * is fine

    Fix: it hides where names come from.

Easy to hard

Questions interviewers ask, easy to hard

  1. What are the application context and the request context in Flask?
  2. Why do people get Working outside of application context?
  3. How would you restructure a Flask app with a circular import?
  4. Users see someone else's orders after we moved to Gunicorn with threads. Why?
  5. What do you do in the first hour?

Explain it out loud

Explain it out loud to an AI interviewer that follows up, and see where your answer is thin.

Practise by voice

Your first interview is free

Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.

Start your free interview