Skip to content

Commit

Permalink
Allow prepare to be async
Browse files Browse the repository at this point in the history
  • Loading branch information
Adrià Casajús committed Oct 3, 2012
1 parent 20deb5c commit 74d8431
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion tornado/web.py
Expand Up @@ -112,6 +112,8 @@ def __init__(self, application, request, **kwargs):
self._headers_written = False
self._finished = False
self._auto_finish = True
self._prepared = False
self._auto_run = True
self._transforms = None # will be set in _execute
self.ui = ObjectDict((n, self._ui_method(m)) for n, m in
application.ui_methods.iteritems())
Expand Down Expand Up @@ -1057,6 +1059,16 @@ def _execute(self, transforms, *args, **kwargs):
self.application.settings.get("xsrf_cookies"):
self.check_xsrf_cookie()
self.prepare()
self._prepared = True
self._method_args = ( args, kwargs )
if self._auto_run:
self.end_prepare()
except Exception, e:
self._handle_request_exception(e)

def _end_prepare( self ):
try:
args, kwargs = self._method_args
if not self._finished:
args = [self.decode_argument(arg) for arg in args]
kwargs = dict((k, self.decode_argument(v, name=k))
Expand Down Expand Up @@ -1156,7 +1168,10 @@ def _on_download(self, response):
def wrapper(self, *args, **kwargs):
if self.application._wsgi:
raise Exception("@asynchronous is not supported for WSGI apps")
self._auto_finish = False
if not self._prepared:
self._auto_run = False
else:
self._auto_finish = False
with stack_context.ExceptionStackContext(
self._stack_context_handle_exception):
return method(self, *args, **kwargs)
Expand Down

0 comments on commit 74d8431

Please sign in to comment.