'yield from' 内部异步函数 Python 3.6.5 aiohttp

'yield from' inside async function Python 3.6.5 aiohttp

语法错误:'yield from' 内部异步函数

async def handle(request):
    for m in (yield from request.post()):
        print(m)
    return web.Response()

之前用过python3.5,发现pep525,安装python3.6.5,还是报错

您正在使用新的 async/await 语法来定义和执行协同例程,但尚未完全切换。这里需要使用await

async def handle(request):
    post_data = await request.post()
    for m in post_data:
        print(m)
    return web.Response()

如果您想坚持使用旧的、Python 3.5 之前的语法,请使用 @asyncio.coroutine decorator 将您的函数标记为协程,删除 async 关键字,然后使用yield from 而不是 await:

@async.coroutine
def handle(request):
    post_data = yield from request.post()
    for m in post_data:
        print(m)
    return web.Response()

但是这种语法正在被淘汰,并且不像新语法那样易于发现和阅读。只有在需要编写与旧 Python 版本兼容的代码时才应使用此表单。