]> git.ipfire.org Git - thirdparty/tornado.git/commitdiff
Create the StackContext in @asynchronous instead of on all requests,
authorBen Darnell <ben@bendarnell.com>
Sun, 3 Jul 2011 18:06:27 +0000 (11:06 -0700)
committerBen Darnell <ben@bendarnell.com>
Sun, 3 Jul 2011 18:06:27 +0000 (11:06 -0700)
to improve performance in the synchronous case.

tornado/stack_context.py
tornado/web.py

index 7504cd783c30733b57a7344a1b8acc787680a10b..58964d5e524a91019aa609fc813b2470059016a3 100644 (file)
@@ -183,7 +183,10 @@ def wrap(fn):
                 callback(*args, **kwargs)
         else:
             callback(*args, **kwargs)
-    return _StackContextWrapper(wrapped, fn, _state.contexts)
+    if _state.contexts:
+        return _StackContextWrapper(wrapped, fn, _state.contexts)
+    else:
+        return _StackContextWrapper(fn)
 
 @contextlib.contextmanager
 def _nested(*managers):
index d436975491919b690c9159b8a013089ec5d61378..205cf645cb9edb6954efe0ac757da2446faf1b06 100644 (file)
@@ -909,8 +909,7 @@ class RequestHandler(object):
     def _execute(self, transforms, *args, **kwargs):
         """Executes this request with the given output transforms."""
         self._transforms = transforms
-        with stack_context.ExceptionStackContext(
-            self._stack_context_handle_exception):
+        try:
             if self.request.method not in self.SUPPORTED_METHODS:
                 raise HTTPError(405)
             # If XSRF cookies are turned on, reject form submissions without
@@ -926,6 +925,8 @@ class RequestHandler(object):
                 getattr(self, self.request.method.lower())(*args, **kwargs)
                 if self._auto_finish and not self._finished:
                     self.finish()
+        except Exception, e:
+            self._handle_request_exception(e)
 
     def _generate_headers(self):
         lines = [utf8(self.request.version + " " +
@@ -1004,7 +1005,9 @@ def asynchronous(method):
         if self.application._wsgi:
             raise Exception("@asynchronous is not supported for WSGI apps")
         self._auto_finish = False
-        return method(self, *args, **kwargs)
+        with stack_context.ExceptionStackContext(
+            self._stack_context_handle_exception):
+            return method(self, *args, **kwargs)
     return wrapper