原因:
1,coroutine容易与正常的generators弄混
2,一个function是否为coroutine由函数体内是否有yield 或者yield from 决定,这不科学。
3,如果在语法上允许yield的地方才能进行异步调用,那诸如with和for语句中都不能执行异步了。
咋解决呢,把coroutine当成一个native的Python语言特性,与generator完全独立。
Native coroutines及其新的语法使得在异步条件下定义context manager(上下文管理器)和iteration protocols(迭代器协议)成为可能(也就是with和for了)。
通过 async with语句可以使得Python程序在进入和退出runtime context(运行时上下文)时,执行异步调用;
通过 async for 语句使得可以在迭代器中执行异步调用。(老外真是的,老是 make it possible).
语法定义
假定你已经知道:
* Python中coroutines的实现。 implementation of coroutines in Python ( PEP 342 and PEP 380 ).
* 一些要被改变的语法来自asyncio框架和"Cofunctions"提议(已经悲剧了)。Motivation for the syntax changes proposed here comes from the asyncio framework ( PEP 3156 ) and the "Cofunctions" proposal ( PEP 3152 , now rejected in favor of this specification).
新定义的c