SyntaxError:异步函数内部的'yield from'
SyntaxError: 'yield from' inside async function
async def handle(request): for m in (yield from request.post()): print(m) return web.Response()以前使用过python3.5,找到pep525,安装python3.6.5,仍然收到此错误。
Used python3.5 before, found pep525, install python3.6.5 and still receive this error.
推荐答案您正在使用新的 async / await 语法来定义和执行协同例程,但尚未完成开关。您需要在此处使用 await
You are using the new async/await syntax to define and execute co-routines, but have not made a full switch. You need to use await here:
async def handle(request): post_data = await request.post() for m in post_data: print(m) return web.Response()如果您想使用Python 3.5之前的旧语法,请使用 @ asyncio.coroutine 装饰器,删除 async 关键字,并使用 yield from 代替 await :
If you wanted to stick to the old, pre-Python 3.5 syntax, then mark your function as a coroutine with the @asyncio.coroutine decorator, drop the async keyword, and use yield from instead of await:
@async.coroutine def handle(request): post_data = yield from request.post() for m in post_data: print(m) return web.Response(),但是此语法正在逐步淘汰,并且不像新语法那样容易发现和可读。仅在需要编写与旧版本Python兼容的代码时,才应使用此表单。
but this syntax is being phased out, and is not nearly as discoverable and readable as the new syntax. You should only use this form if you need to write code that is compatible with older Python versions.
更多推荐
异步函数Python 3.6.5 aiohttp中的'yield from'
发布评论