Skip to content
Myr

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

5 files+24 added−5 removed

modified:docs/patterns/sqlite3.rst+9 added-1 removed
22 unmodified lines
23 g.db = connect_db()2425 @app.teardown_request26 def teardown_request(exception):27 if hasattr(g, 'db'):27 g.db.close()28 g.db.close()2930.. note::3132 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.3637Connect on Demand38-----------------3940The downside of this approach is that this will only work if Flask
79 unmodified lines
modified:docs/reqcontext.rst+9 added-1 removed
129 unmodified lines
130 functions which have the chance to replace it or modify it in place.1311324. 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).137138Now what happens on errors? In production mode if an exception is not139caught, the 500 internal server handler is called. In development mode140however the exception is not further processed and bubbles up to the WSGI
43 unmodified lines
184>>> ctx.pop()185this runs after request186>>>187188Keep in mind that teardown callbacks are always executed, even if189before-request callbacks were not executed yet but an exception happened.190Certain parts of the test system might also temporarily create a request191context without calling the before-request handlers. Make sure to write192your teardown-request handlers in a way that they will never fail.193194.. _notes-on-proxies:195196Notes On Proxies197----------------
42 unmodified lines
modified:docs/upgrading.rst+2 added-1 removed
140 unmodified lines
141You are now encouraged to use this instead::142143 @app.teardown_request144 def after_request(exception):145 if hasattr(g, 'db'):145 g.db.close()146 g.db.close()147148On the upside this change greatly improves the internal code flow and149makes it easier to customize the dispatching and error handling. This150makes it now a lot easier to write unit tests as you can prevent closing
169 unmodified lines
modified:examples/flaskr/flaskr.py+2 added-1 removed
48 unmodified lines
4950@app.teardown_request51def 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()555657@app.route('/')58def show_entries():
40 unmodified lines
modified:examples/minitwit/minitwit.py+2 added-1 removed
83 unmodified lines
8485@app.teardown_request86def 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()909192@app.route('/')93def timeline():
155 unmodified lines