Browse Source

Updated the docs and examples to non-failing teardown handlers

pull/308/merge
Armin Ronacher 14 years ago
parent
commit
d3ca55177a
  1. 10
      docs/patterns/sqlite3.rst
  2. 10
      docs/reqcontext.rst
  3. 3
      docs/upgrading.rst
  4. 3
      examples/flaskr/flaskr.py
  5. 3
      examples/minitwit/minitwit.py

10
docs/patterns/sqlite3.rst

@ -24,7 +24,15 @@ So here is a simple example of how you can use SQLite 3 with Flask::
@app.teardown_request @app.teardown_request
def teardown_request(exception): def teardown_request(exception):
g.db.close() if hasattr(g, 'db'):
g.db.close()
.. note::
Please keep in mind that the teardown request functions are always
executed, even if a before-request handler failed or was never
executed. Because of this we have to make sure here that the database
is there before we close it.
Connect on Demand Connect on Demand
----------------- -----------------

10
docs/reqcontext.rst

@ -131,7 +131,9 @@ understand what is actually happening. The new behavior is quite simple:
4. At the end of the request the :meth:`~flask.Flask.teardown_request` 4. At the end of the request the :meth:`~flask.Flask.teardown_request`
functions are executed. This always happens, even in case of an functions are executed. This always happens, even in case of an
unhandled exception down the road. unhandled exception down the road or if a before-request handler was
not executed yet or at all (for example in test environments sometimes
you might want to not execute before-request callbacks).
Now what happens on errors? In production mode if an exception is not Now what happens on errors? In production mode if an exception is not
caught, the 500 internal server handler is called. In development mode caught, the 500 internal server handler is called. In development mode
@ -183,6 +185,12 @@ It's easy to see the behavior from the command line:
this runs after request this runs after request
>>> >>>
Keep in mind that teardown callbacks are always executed, even if
before-request callbacks were not executed yet but an exception happened.
Certain parts of the test system might also temporarily create a request
context without calling the before-request handlers. Make sure to write
your teardown-request handlers in a way that they will never fail.
.. _notes-on-proxies: .. _notes-on-proxies:
Notes On Proxies Notes On Proxies

3
docs/upgrading.rst

@ -142,7 +142,8 @@ You are now encouraged to use this instead::
@app.teardown_request @app.teardown_request
def after_request(exception): def after_request(exception):
g.db.close() if hasattr(g, 'db'):
g.db.close()
On the upside this change greatly improves the internal code flow and On the upside this change greatly improves the internal code flow and
makes it easier to customize the dispatching and error handling. This makes it easier to customize the dispatching and error handling. This

3
examples/flaskr/flaskr.py

@ -50,7 +50,8 @@ def before_request():
@app.teardown_request @app.teardown_request
def teardown_request(exception): def teardown_request(exception):
"""Closes the database again at the end of the request.""" """Closes the database again at the end of the request."""
g.db.close() if hasattr(g, 'db'):
g.db.close()
@app.route('/') @app.route('/')

3
examples/minitwit/minitwit.py

@ -85,7 +85,8 @@ def before_request():
@app.teardown_request @app.teardown_request
def teardown_request(exception): def teardown_request(exception):
"""Closes the database again at the end of the request.""" """Closes the database again at the end of the request."""
g.db.close() if hasattr(g, 'db'):
g.db.close()
@app.route('/') @app.route('/')

Loading…
Cancel
Save