]> git.ipfire.org Git - thirdparty/tornado.git/commitdiff
Add RequestHandler.on_finish method.
authorBen Darnell <ben@bendarnell.com>
Wed, 18 Jan 2012 08:43:38 +0000 (00:43 -0800)
committerBen Darnell <ben@bendarnell.com>
Wed, 18 Jan 2012 08:43:38 +0000 (00:43 -0800)
Closes #367.

tornado/web.py
website/sphinx/overview.rst
website/sphinx/releases/next.rst

index b403afe24b057af52debf0c8d35159e7b4bfa7e5..3ebab8cac5d5f89326cc63c614f141e37fd58e2b 100644 (file)
@@ -171,18 +171,31 @@ class RequestHandler(object):
         raise HTTPError(405)
 
     def prepare(self):
-        """Called before the actual handler method.
+        """Called at the beginning of a request before `get`/`post`/etc.
 
-        Useful to override in a handler if you want a common bottleneck for
-        all of your requests.
+        Override this method to perform common initialization regardless
+        of the request method.
+        """
+        pass
+
+    def on_finish(self):
+        """Called after the end of a request.
+
+        Override this method to perform cleanup, logging, etc.
+        This method is a counterpart to `prepare`.  ``on_finish`` may
+        not produce any output, as it is called after the response
+        has been sent to the client.
         """
         pass
 
     def on_connection_close(self):
         """Called in async handlers if the client closed the connection.
 
-        You may override this to clean up resources associated with
-        long-lived connections.
+        Override this to clean up resources associated with
+        long-lived connections.  Note that this method is called only if
+        the connection was closed during asynchronous processing; if you
+        need to do cleanup after every request override `on_finish`
+        instead.
 
         Proxies may keep a connection open for a time (perhaps
         indefinitely) after the client has gone away, so this method
@@ -656,6 +669,7 @@ class RequestHandler(object):
             self.request.finish()
             self._log()
         self._finished = True
+        self.on_finish()
 
     def send_error(self, status_code=500, **kwargs):
         """Sends the given HTTP error code to the browser.
index c1dad40f43168ad16808c65fb381b25f927cfc95..29c88ee745cde806985d27ed3387fc24b40b9cf5 100644 (file)
@@ -140,6 +140,9 @@ place:
 4. One of the HTTP methods is called: ``get()``, ``post()``, ``put()``,
    etc. If the URL regular expression contains capturing groups, they
    are passed as arguments to this method.
+5. When the request is finished, ``on_finish()`` is called.  For synchronous
+   handlers this is immediately after ``get()`` (etc) return; for
+   asynchronous handlers it is after the call to ``finish()``.
 
 Here is an example demonstrating the ``initialize()`` method:
 
index 6fbbf1eef2818dc959996f6857ad276dd9a97c38..d1d0802faa4f527a46fca0d20b9f86ea54f628e5 100644 (file)
@@ -49,6 +49,15 @@ Backwards-incompatible changes
   ``{% comment %}`` directive, these can wrap other template directives).
 * Template directives may now span multiple lines.
 
+``tornado.web``
+~~~~~~~~~~~~~~~
+
+* Now behaves better when given malformed ``Cookie`` headers
+* `RequestHandler.redirect` now has a ``status`` argument to send
+  status codes other than 301 and 302.
+* New method `RequestHandler.on_finish` may be overridden for post-request
+  processing (as a counterpart to `RequestHandler.prepare`)
+
 ``tornado.websocket``
 ~~~~~~~~~~~~~~~~~~~~~
 
@@ -67,9 +76,6 @@ Other modules
   responses with no content, or empty ``POST``/``PUT`` response bodies.
 * `tornado.platform.twisted` compatibility has been significantly improved.
   Twisted version 11.1.0 is now supported in addition to 11.0.0.
-* `tornado.web` now behaves better when given malformed ``Cookie`` headers
-* `RequestHandler.redirect` now has a ``status`` argument to send
-  status codes other than 301 and 302.
 * `tornado.testing.main` supports a new flag ``--exception_on_interrupt``,
   which can be set to false to make ``Ctrl-C`` kill the process more
   reliably (at the expense of stack traces when it does so).