2
鏈考慮下面的代碼:aiohttp:裝飾序列
from aiohttp_mako import template
def authorize():
def wrapper(func):
@asyncio.coroutine
@functools.wraps(func)
def wrapped(*args):
allowed = # Some auth stuff
if not allowed:
return HTTPUnauthorized()
return func()
return wrapped
return wrapper
@authorize()
@template('admin.mak')
async def admin(request):
return dict(ok=True)
我希望authorize()
'wrapper
得到template
裝飾爲func
這樣我就可以回到它生成的Response
我authorize
裝飾。但authorize()
'wrapper
採取admin()
協程爲func
而且因爲它試圖返回協程與
File "/Users/etemin/virtualenvs/onlinelux/lib/python3.5/site-packages/aiohttp/web.py", line 306, in _handle
resp = yield from handler(request)
File "/Users/etemin/virtualenvs/onlinelux/lib/python3.5/site-packages/aiohttp_session/__init__.py", line 134, in middleware
raise RuntimeError("Expect response, not {!r}", type(response))
RuntimeError: ('Expect response, not {!r}', <class 'generator'>)
結束。我應該如何使它返回template
修飾器?
謝謝你的迴應。我遇到了一些非常奇怪的事情。 'yield'語法引發了'RuntimeError:('Expect response,not {!r}',)',但同樣的'async/await'語法效果很好!怎麼來的? –
Juggernaut
@Juggernaut:啊,忘了'return',現在加入。對於那個很抱歉! –