]> git.ipfire.org Git - thirdparty/tornado.git/commitdiff
Add a method to RequestHandler that can be used to detect when the connection
authorBen Darnell <bdarnell@beaker.local>
Fri, 8 Jan 2010 03:06:11 +0000 (19:06 -0800)
committerBen Darnell <bdarnell@beaker.local>
Fri, 8 Jan 2010 03:06:11 +0000 (19:06 -0800)
has been closed by the client.

This has always been supported via
self.request.connection.stream.set_close_callback, but putting it in
RequestHandler directly is more discoverable and less fragile.

tornado/web.py

index 4278e1a638a545c6bf8c8a7cba13640088358b02..785eb0087ec65b3acca16a711db9ece4fd614599 100644 (file)
@@ -92,6 +92,8 @@ class RequestHandler(object):
         self.ui["modules"] = _O((n, self._ui_module(n, m)) for n, m in
                                 application.ui_modules.iteritems())
         self.clear()
+        self.request.connection.stream.set_close_callback(
+            self.on_connection_close)
 
     @property
     def settings(self):
@@ -120,6 +122,20 @@ class RequestHandler(object):
         """
         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.
+
+        Note that the select()-based implementation of IOLoop does not detect
+        closed connections and so this method will not be called until
+        you try (and fail) to produce some output.  The epoll- and kqueue-
+        based implementations should detect closed connections even while
+        the request is idle.
+        """
+        pass
+
     def clear(self):
         """Resets all headers and content for this response."""
         self._headers = {