[Python-ideas] async/await in Python

Chris Angelico rosuav at gmail.com
Sat Apr 18 23:29:25 CEST 2015


On Sun, Apr 19, 2015 at 3:39 AM, Yury Selivanov <yselivanov.ml at gmail.com> wrote:
> The internal implementation based on generators is something
> that was absolutely required to cover in the PEP in a great detail,
> but almost everybody will forget about that fact pretty soon.
>
> The point of this PEP is to actually avoid any confusion, and
> to just have two separate concepts: coroutines and generators
> (the latter can still be used as a "coroutine-like" object,
> but hopefully nobody will do that at some point).

Which means that these are three very VERY different things, all of
which happen to be implemented (under the covers) using similar
mechanisms:

async def corout():
    await spam()

def gen():
    yield from [1,2,3]

class iter:
    def __init__(self): self.state = 0
    def __iter__(self): return self
    def __next__(self):
        if self.state == 3: raise StopIteration
        self.state += 1
        return self.state

Technically, a generator object is an iterator, and a coroutine is a
generator, but they're really implementation details. PEP 479 is all
about not expecting "raise StopIteration" to terminate gen(); in the
same way, programmers should not expect "for x in corout()" to do
anything meaningful, because they're completely different things.

Have I understood this correctly?

ChrisA


More information about the Python-ideas mailing list