Coroutine and native coroutine generator

I just read PEP0492 talking about a new approach on coroutines, but PEP could not make me understand the difference between generator-based coroutines and their native ones. Can someone tell me the difference (maybe with examples)?

For what I understand, they use different words (yield / yield from and await / async / yield). I understand that an exit is expected at the end of the native coroutine, but this is also true for generator ones.

+4
source share
3 answers

There is no functional difference. “Native coroutines” using keywords asyncand awaitis just syntactic sugar for what was previously implemented in “generator-based coroutines”.

Use asyncand awaitrecommended in 3.5 docs if there is no need to support older Python versions .

+1
source

, Mike S: CPython , , . , , PEP-492 " ". , , . :

  • , - .
  • await foo , yield from foo , , await , yield from .
  • , .
  • Coroutines , . , await coroutine.

asyncio , .

+4

, . , , , , , , , . , yield ( yield from python 3.3), pythonic.

.

@asyncio.coroutine
def print_sum(x, y):
    result = yield from compute(x, y)

    #write callback code
    print("%s + %s = %s" % (x, y, result))
0

Source: https://habr.com/ru/post/1607422/


All Articles