[Python-ideas] The async API of the future: yield-from

Guido van Rossum guido at python.org
Mon Oct 15 18:09:51 CEST 2012


On Mon, Oct 15, 2012 at 8:32 AM, Nick Coghlan <ncoghlan at gmail.com> wrote:
> My preferred way of thinking of "yield from" is as a simple
> refactoring tool: "Gee, this generator is getting kind of long and
> unwieldy. I'll move this piece out into a separate generator, and use
> yield from to invoke it" or "Hmm, I keep using this same sequence of 3
> or 4 operations. I guess I'll move them out to a separate generator
> and use yield from to invoke it in the appropriate places".

In the NDB world you would say:

"Gee this _tasklet_ is getting kind of long and unwieldy. I'll move
this piece out into a separate _tasklet_, and use _yield_ to invoke
it."

Creating a tasklet is just writing a generator decorated with
@ndb.tasklet -- after using this a bit it becomes total second nature
(I've seen several coworkers pick it up effortlessly).

I'll have to digest your other points about yield vs. yield-from more
carefully -- on the one hand I think it would be cool if yield-from
could give us an even simpler paradigm to write async code than NDB's
version, and that expectation was one of my main reasons to push for
PEP 380's acceptance. On the other hand you bring up some good points
with the as_completed() example (though I have a feeling Greg will
easily sail around it :-).

PS. Unrelated, and please don't respond to this or at least change the
subject if you feel compelled: there seem to be a lot of bad names in
this field. Twisted uses adjectives as nouns (Twisted, Deferred, I
read about another one), "add_done_callback" is too longwinded,
"as_completed" brings absolutely no useful association with it..

-- 
--Guido van Rossum (python.org/~guido)



More information about the Python-ideas mailing list