From: Ben Darnell Date: Sun, 3 Jul 2011 18:06:27 +0000 (-0700) Subject: Create the StackContext in @asynchronous instead of on all requests, X-Git-Tag: v2.1.0~129 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=9c17711e15e1cdc29ed132e29439283fab6a5cd5;p=thirdparty%2Ftornado.git Create the StackContext in @asynchronous instead of on all requests, to improve performance in the synchronous case. --- diff --git a/tornado/stack_context.py b/tornado/stack_context.py index 7504cd783..58964d5e5 100644 --- a/tornado/stack_context.py +++ b/tornado/stack_context.py @@ -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): diff --git a/tornado/web.py b/tornado/web.py index d43697549..205cf645c 100644 --- a/tornado/web.py +++ b/tornado/web.py @@ -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