'yield from' inside async function Python 3.6.5 aiohttp

拜拜、爱过 提交于 2019-12-13 09:07:34

问题


SyntaxError: 'yield from' inside async function

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

Used python3.5 before, found pep525, install python3.6.5 and still receive this error.


回答1:


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()

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()

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.



来源:https://stackoverflow.com/questions/51583968/yield-from-inside-async-function-python-3-6-5-aiohttp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!