Updated the docs and examples to non-failing teardown handlers
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 485a6c3 · Browse files · 5 files·+24 added−5 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
5 files+24 added−5 removed
- other:+24 added−5 removed
Provenance
- Commit
d3ca551- Tree
da6841a- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
485a6c3
5 files changed+24 added−5 removed
modified:docs/patterns/sqlite3.rst+9 added-1 removed
22 unmodified lines23 g.db = connect_db()24 25 @app.teardown_request26 def teardown_request(exception):27+ if hasattr(g, 'db'):27- g.db.close()28+ g.db.close()29 30+.. note::31+32+ Please keep in mind that the teardown request functions are always33+ executed, even if a before-request handler failed or was never34+ executed. Because of this we have to make sure here that the database35+ is there before we close it.36+37 Connect on Demand38 -----------------39 40 The downside of this approach is that this will only work if Flask79 unmodified linesmodified:docs/reqcontext.rst+9 added-1 removed
129 unmodified lines130 functions which have the chance to replace it or modify it in place.131 132 4. At the end of the request the :meth:`~flask.Flask.teardown_request`133 functions are executed. This always happens, even in case of an134- unhandled exception down the road.134+ unhandled exception down the road or if a before-request handler was135+ not executed yet or at all (for example in test environments sometimes136+ you might want to not execute before-request callbacks).137 138 Now what happens on errors? In production mode if an exception is not139 caught, the 500 internal server handler is called. In development mode140 however the exception is not further processed and bubbles up to the WSGI43 unmodified lines184 >>> ctx.pop()185 this runs after request186 >>>187 188+Keep in mind that teardown callbacks are always executed, even if189+before-request callbacks were not executed yet but an exception happened.190+Certain parts of the test system might also temporarily create a request191+context without calling the before-request handlers. Make sure to write192+your teardown-request handlers in a way that they will never fail.193+194 .. _notes-on-proxies:195 196 Notes On Proxies197 ----------------42 unmodified linesmodified:docs/upgrading.rst+2 added-1 removed
140 unmodified lines141 You are now encouraged to use this instead::142 143 @app.teardown_request144 def after_request(exception):145+ if hasattr(g, 'db'):145- g.db.close()146+ g.db.close()147 148 On the upside this change greatly improves the internal code flow and149 makes it easier to customize the dispatching and error handling. This150 makes it now a lot easier to write unit tests as you can prevent closing169 unmodified linesmodified:examples/flaskr/flaskr.py+2 added-1 removed
48 unmodified lines49 50 @app.teardown_request51 def teardown_request(exception):52 """Closes the database again at the end of the request."""53+ if hasattr(g, 'db'):53- g.db.close()54+ g.db.close()55 56 57 @app.route('/')58 def show_entries():40 unmodified linesmodified:examples/minitwit/minitwit.py+2 added-1 removed
83 unmodified lines84 85 @app.teardown_request86 def teardown_request(exception):87 """Closes the database again at the end of the request."""88+ if hasattr(g, 'db'):88- g.db.close()89+ g.db.close()90 91 92 @app.route('/')93 def timeline():155 unmodified lines