106
|
1 |
import asyncio
|
|
2 |
import qmsk.web.application
|
|
3 |
import werkzeug
|
|
4 |
|
|
5 |
class Handler (qmsk.web.application.Handler) :
|
|
6 |
@asyncio.coroutine
|
|
7 |
def process_async (self, **params) :
|
|
8 |
"""
|
|
9 |
Process request asynchronously.
|
|
10 |
|
|
11 |
May optionally return a Response, to e.g. redirect after POST.
|
|
12 |
"""
|
|
13 |
|
|
14 |
pass
|
|
15 |
|
|
16 |
@asyncio.coroutine
|
|
17 |
def respond_async (self, **params):
|
|
18 |
response = yield from self.process_async(**params)
|
|
19 |
|
|
20 |
if response :
|
|
21 |
return response
|
|
22 |
|
|
23 |
return self.respond(**params)
|
|
24 |
|
|
25 |
class Application (qmsk.web.application.Application) :
|
|
26 |
@asyncio.coroutine
|
|
27 |
def __call__ (self, environ, start_response):
|
|
28 |
request = werkzeug.Request(environ)
|
|
29 |
|
|
30 |
handler, params = self.lookup(request)
|
|
31 |
|
|
32 |
try:
|
|
33 |
handler.init()
|
|
34 |
|
|
35 |
response = yield from handler.respond_async(**params)
|
|
36 |
|
|
37 |
finally:
|
|
38 |
handler.cleanup()
|
|
39 |
|
|
40 |
return response(environ, start_response)
|