Merge pull request #3434 from pallets/eager-load
Before adoption: this commit came in with the history the project adopted, so no landing records it.
lazy load app on reload only
authored by David Lord · committed by GitHub · parents a83d6d2, a671e47 · Browse files · 4 files·+72 added−41 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+72 added−41 removed
- other:+72 added−41 removed
Provenance
- Commit
bfa9ace- Tree
c606773- Author
David Lord <davidism@gmail.com>- Committer
GitHub <noreply@github.com>- Parent 1
a83d6d2- Parent 2
a671e47
4 files changed+72 added−41 removed
modified:docs/quickstart.rst+3 added-3 removed
106 unmodified lines107 108 Versions of Flask older than 0.11 use to have different ways to start the109 application. In short, the :command:`flask` command did not exist, and110 neither did :command:`python -m flask`. In that case you have two options:111-either upgrade to newer Flask versions or have a look at the :ref:`server`112-docs to see the alternative method for running a server.111+either upgrade to newer Flask versions or have a look at :doc:`/server`112+to see the alternative method for running a server.113 114 Invalid Import Name115 ```````````````````116 35 unmodified lines152 153 You can also control debug mode separately from the environment by154 exporting ``FLASK_DEBUG=1``.155 156-There are more parameters that are explained in the :ref:`server` docs.156+There are more parameters that are explained in :doc:`/server`.157 158 .. admonition:: Attention159 160 Even though the interactive debugger does not work in forking environments740 unmodified linesmodified:docs/server.rst+60 added-33 removed
1-.. _server:1+.. currentmodule:: flask2 3 Development Server4 ==================5 6-.. currentmodule:: flask6+Flask provides a ``run`` command to run the application with a7+development server. In development mode, this server provides an8+interactive debugger and will reload when code is changed.9 8-Starting with Flask 0.11 there are multiple built-in ways to run a9-development server. The best one is the :command:`flask` command line utility10-but you can also continue using the :meth:`Flask.run` method.10+.. warning::11 12+ Do not use the development server when deploying to production. It13+ is intended for use only during local development. It is not14+ designed to be particularly efficient, stable, or secure.15+16+ See :doc:`/deploying/index` for deployment options.17+18 Command Line19 ------------20 15-The :command:`flask` command line script (:ref:`cli`) is strongly16-recommended for development because it provides a superior reload17-experience due to how it loads the application. The basic usage is like18-this::21+The ``flask run`` command line script is the recommended way to run the22+development server. It requires setting the ``FLASK_APP`` environment23+variable to point to your application, and ``FLASK_ENV=development`` to24+fully enable development mode.25 26+.. code-block:: text27+20- $ export FLASK_APP=my_application28+ $ export FLASK_APP=hello29 $ export FLASK_ENV=development30 $ flask run31 32 This enables the development environment, including the interactive33 debugger and reloader, and then starts the server on26-*http://localhost:5000/*.34+http://localhost:5000/. Use ``flask run --help`` to see the available35+options, and :doc:`/cli` for detailed instructions about configuring36+and using the CLI.37 28-The individual features of the server can be controlled by passing more29-arguments to the ``run`` option. For instance the reloader can be30-disabled::31-32- $ flask run --no-reload33-38 .. note::39 36- Prior to Flask 1.0 the :envvar:`FLASK_ENV` environment variable was37- not supported and you needed to enable debug mode by exporting40+ Prior to Flask 1.0 the ``FLASK_ENV`` environment variable was not41+ supported and you needed to enable debug mode by exporting42 ``FLASK_DEBUG=1``. This can still be used to control debug mode, but43 you should prefer setting the development environment as shown44 above.45 46+47+Lazy or Eager Loading48+~~~~~~~~~~~~~~~~~~~~~49+50+When using the ``flask run`` command with the reloader, the server will51+continue to run even if you introduce syntax errors or other52+initialization errors into the code. Accessing the site will show the53+interactive debugger for the error, rather than crashing the server.54+This feature is called "lazy loading".55+56+If a syntax error is already present when calling ``flask run``, it will57+fail immediately and show the traceback rather than waiting until the58+site is accessed. This is intended to make errors more visible initially59+while still allowing the server to handle errors on reload.60+61+To override this behavior and always fail immediately, even on reload,62+pass the ``--eager-loading`` option. To always keep the server running,63+even on the initial call, pass ``--lazy-loading``.64+65+66 In Code67 -------68 45-The alternative way to start the application is through the46-:meth:`Flask.run` method. This will immediately launch a local server47-exactly the same way the :command:`flask` script does.69+As an alternative to the ``flask run`` command, the development server70+can also be started from Python with the :meth:`Flask.run` method. This71+method takes arguments similar to the CLI options to control the server.72+The main difference from the CLI command is that the server will crash73+if there are errors when reloading.74 49-Example::75+``debug=True`` can be passed to enable the debugger and reloader, but76+the ``FLASK_ENV=development`` environment variable is still required to77+fully enable development mode.78 51- if __name__ == '__main__':52- app.run()79+Place the call in a main block, otherwise it will interfere when trying80+to import and run the application with a production server later.81 54-This works well for the common case but it does not work well for55-development which is why from Flask 0.11 onwards the :command:`flask`56-method is recommended. The reason for this is that due to how the reload57-mechanism works there are some bizarre side-effects (like executing58-certain code twice, sometimes crashing without message or dying when a59-syntax or import error happens).82+.. code-block:: python83 61-It is however still a perfectly valid method for invoking a non automatic62-reloading application.84+ if __name__ == "__main__":85+ app.run(debug=True)86+87+.. code-block:: text88+89+ $ python hello.pymodified:src/flask/cli.py+6 added-5 removed
294 unmodified lines295 it is recorded and shown as part of the WSGI handling which in case296 of the Werkzeug debugger means that it shows up in the browser.297 """298 299- def __init__(self, loader, use_eager_loading=False):299+ def __init__(self, loader, use_eager_loading=None):300 self.loader = loader301 self._app = None302 self._lock = Lock()303 self._bg_loading_exc_info = None304+305+ if use_eager_loading is None:306+ use_eager_loading = os.environ.get("WERKZEUG_RUN_MAIN") != "true"307+308 if use_eager_loading:309 self._load_unlocked()310 else:311 self._load_in_background()493 unmodified lines805 help="Enable or disable the debugger. By default the debugger "806 "is active if debug is enabled.",807 )808 @click.option(805- "--eager-loading/--lazy-loader",809+ "--eager-loading/--lazy-loading",810 default=None,811 help="Enable or disable eager loading. By default eager "812 "loading is enabled if the reloader is disabled.",813 )30 unmodified lines844 845 if debugger is None:846 debugger = debug847 844- if eager_loading is None:845- eager_loading = not reload846-848 show_server_banner(get_env(), debug, info.app_import_path, eager_loading)849 app = DispatchingApp(info.load_app, use_eager_loading=eager_loading)850 851 from werkzeug.serving import run_simple121 unmodified linesmodified:CHANGES.rst+3 added
8 unmodified lines9 setting the session cookie name dynamically. :pr:`3369`10 - Add :meth:`Config.from_file` to load config using arbitrary file11 loaders, such as ``toml.load`` or ``json.load``.12 :meth:`Config.from_json` is deprecated in favor of this. :pr:`3398`13+- The ``flask run`` command will only defer errors on reload. Errors14+ present during the initial call will cause the server to exit with15+ the traceback immediately. :issue:`3431`16 17 18 Version 1.1.219 -------------1004 unmodified lines