Skip to content
Myr

Added support for background loading of the application to speed up initial requests in dev mode.

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Armin Ronacher · committed by Armin Ronacher · parent 670d2a4 · Browse files · 1 file+25 added−4 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+25 added−4 removed

  • other:+25 added−4 removed

1 file+25 added−4 removed

modified:flask/cli.py+25 added-4 removed
9 unmodified lines
10"""1112import os13import sys14from threading import Lock14from threading import Lock, Thread15from functools import update_wrapper1617import click18
79 unmodified lines
9899100class DispatchingApp(object):101 """Special application that dispatches to a flask application which102 is imported by name on first request. This is safer than importing103 the application upfront because it means that we can forward all104 errors for import problems into the browser as error.102 is imported by name in a background thread. If an error happens103 it is is recorded and shows as part of the WSGI handling which in case104 of the Werkzeug debugger means that it shows up in the browser.105 """106107 def __init__(self, loader, use_eager_loading=False):108 self.loader = loader109 self._app = None110 self._lock = Lock()111 self._bg_loading_exc_info = None112 if use_eager_loading:113 self._load_unlocked()114 else:115 self._load_in_background()116117 def _load_in_background(self):118 def _load_app():119 with self._lock:120 try:121 self._load_unlocked()122 except Exception:123 self._bg_loading_exc_info = sys.exc_info()124 t = Thread(target=_load_app, args=())125 t.start()126127 def _flush_bg_loading_exception(self):128 exc_info = self._bg_loading_exc_info129 if exc_info is not None:130 self._bg_loading_exc_info = None131 raise exc_info[0], exc_info[1], exc_info[2]132133 def _load_unlocked(self):134 self._app = rv = self.loader()135 self._bg_loading_exc_info = None136 return rv137138 def __call__(self, environ, start_response):139 if self._app is not None:140 return self._app(environ, start_response)141 self._flush_bg_loading_exception()142 with self._lock:143 if self._app is not None:144 rv = self._app145 else:
347 unmodified lines