异步函数Python 3.6.5 aiohttp中的'yield from' [英] 'yield from' inside async function Python 3.6.5 aiohttp

查看:595
本文介绍了异步函数Python 3.6.5 aiohttp中的'yield from'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆