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.
There is no functional difference. “Native coroutines” using keywords asyncand awaitis just syntactic sugar for what was previously implemented in “generator-based coroutines”.
async
await
Use asyncand awaitrecommended in 3.5 docs if there is no need to support older Python versions .
, Mike S: CPython , , . , , PEP-492 " ". , , . :
await foo
yield from foo
yield from
asyncio , .
asyncio
, . , , , , , , , . , yield ( yield from python 3.3), pythonic.
yield
.
@asyncio.coroutine def print_sum(x, y): result = yield from compute(x, y) #write callback code print("%s + %s = %s" % (x, y, result))
Source: https://habr.com/ru/post/1607422/More articles:Is there a way to prettily format or decorate the NSIS script source code? - code-formattingmoving image to another image on hover - javascriptCreating an autoresist printout - objective-cHow to convert a column to rows in pandas? - pandasWhat happens if RDD cannot fit into Spark's memory? - scalaSwift 2: protocol extension UITableViewDataSource - iosUsing object properties that are not specified in Object Browser - objectto fix laravel 5 session expiring after updating or switching to another page? - laravelHow to use an optional attribute for alleged form arrays - fortranRetrieving artifacts created by a remote trigger - jenkinsAll Articles