Skip to content

Commit 6dfc1b7

Browse files
committed
Add RequestHandler.on_finish method.
Closes tornadoweb#367.
1 parent af940f4 commit 6dfc1b7

File tree

3 files changed

+31
-8
lines changed

3 files changed

+31
-8
lines changed

tornado/web.py

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -171,18 +171,31 @@ def options(self, *args, **kwargs):
171171
raise HTTPError(405)
172172

173173
def prepare(self):
174-
"""Called before the actual handler method.
174+
"""Called at the beginning of a request before `get`/`post`/etc.
175175
176-
Useful to override in a handler if you want a common bottleneck for
177-
all of your requests.
176+
Override this method to perform common initialization regardless
177+
of the request method.
178+
"""
179+
pass
180+
181+
def on_finish(self):
182+
"""Called after the end of a request.
183+
184+
Override this method to perform cleanup, logging, etc.
185+
This method is a counterpart to `prepare`. ``on_finish`` may
186+
not produce any output, as it is called after the response
187+
has been sent to the client.
178188
"""
179189
pass
180190

181191
def on_connection_close(self):
182192
"""Called in async handlers if the client closed the connection.
183193
184-
You may override this to clean up resources associated with
185-
long-lived connections.
194+
Override this to clean up resources associated with
195+
long-lived connections. Note that this method is called only if
196+
the connection was closed during asynchronous processing; if you
197+
need to do cleanup after every request override `on_finish`
198+
instead.
186199
187200
Proxies may keep a connection open for a time (perhaps
188201
indefinitely) after the client has gone away, so this method
@@ -656,6 +669,7 @@ def finish(self, chunk=None):
656669
self.request.finish()
657670
self._log()
658671
self._finished = True
672+
self.on_finish()
659673

660674
def send_error(self, status_code=500, **kwargs):
661675
"""Sends the given HTTP error code to the browser.

website/sphinx/overview.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,9 @@ place:
140140
4. One of the HTTP methods is called: ``get()``, ``post()``, ``put()``,
141141
etc. If the URL regular expression contains capturing groups, they
142142
are passed as arguments to this method.
143+
5. When the request is finished, ``on_finish()`` is called. For synchronous
144+
handlers this is immediately after ``get()`` (etc) return; for
145+
asynchronous handlers it is after the call to ``finish()``.
143146

144147
Here is an example demonstrating the ``initialize()`` method:
145148

website/sphinx/releases/next.rst

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,15 @@ Backwards-incompatible changes
4949
``{% comment %}`` directive, these can wrap other template directives).
5050
* Template directives may now span multiple lines.
5151

52+
``tornado.web``
53+
~~~~~~~~~~~~~~~
54+
55+
* Now behaves better when given malformed ``Cookie`` headers
56+
* `RequestHandler.redirect` now has a ``status`` argument to send
57+
status codes other than 301 and 302.
58+
* New method `RequestHandler.on_finish` may be overridden for post-request
59+
processing (as a counterpart to `RequestHandler.prepare`)
60+
5261
``tornado.websocket``
5362
~~~~~~~~~~~~~~~~~~~~~
5463

@@ -67,9 +76,6 @@ Other modules
6776
responses with no content, or empty ``POST``/``PUT`` response bodies.
6877
* `tornado.platform.twisted` compatibility has been significantly improved.
6978
Twisted version 11.1.0 is now supported in addition to 11.0.0.
70-
* `tornado.web` now behaves better when given malformed ``Cookie`` headers
71-
* `RequestHandler.redirect` now has a ``status`` argument to send
72-
status codes other than 301 and 302.
7379
* `tornado.testing.main` supports a new flag ``--exception_on_interrupt``,
7480
which can be set to false to make ``Ctrl-C`` kill the process more
7581
reliably (at the expense of stack traces when it does so).

0 commit comments

Comments
 (0)