qmsk/web/async.py
changeset 106 89bd5028b150
child 107 05707929ff6f
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/qmsk/web/async.py	Thu Jan 29 22:16:16 2015 +0200
@@ -0,0 +1,40 @@
+import asyncio
+import qmsk.web.application
+import werkzeug
+
+class Handler (qmsk.web.application.Handler) :
+    @asyncio.coroutine
+    def process_async (self, **params) :
+        """
+            Process request asynchronously.
+
+            May optionally return a Response, to e.g. redirect after POST.
+        """
+
+        pass
+
+    @asyncio.coroutine
+    def respond_async (self, **params):
+        response = yield from self.process_async(**params)
+
+        if response :
+            return response
+
+        return self.respond(**params)
+
+class Application (qmsk.web.application.Application) :
+    @asyncio.coroutine
+    def __call__ (self, environ, start_response):
+        request = werkzeug.Request(environ)
+
+        handler, params = self.lookup(request)
+
+        try:
+            handler.init()
+
+            response = yield from handler.respond_async(**params)
+
+        finally:
+            handler.cleanup()
+
+        return response(environ, start_response)