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

Nick Coghlan ncoghlan at gmail.com
Mon Oct 15 17:32:46 CEST 2012


On Tue, Oct 16, 2012 at 1:16 AM, Calvin Spealman <ironfroggy at gmail.com> wrote:
> An example of this is a task which makes multiple requests, but only needs to
> wait for the results from less-than-all of them before returning. It
> might still want
> the other tasks to complete, even if it won't do anything with the results.
>
> yield-from semantics won't allow a called task to continue, if needed, after the
> calling task itself has completed.
>
> Is there another way these semantics could be expressed?

Sure, did you see my as_completed example? You couldn't use "yield
from" for that, you'd need to use an ordinary iterator and an explicit
yield in the body of the loop (this is why I disagree with Greg that
"yield from" can serve as the one true API - it doesn't handle partial
iteration, and it doesn't handle pre- or post- processing around the
suspension points while iterating).

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".

Compare that with the almost identical equivalents when refactoring a
function to call a helper function instead of doing everything inline:
"Gee, this function is getting kind of long and unwieldy. I'll move
this piece out into a separate function, and call it" or "Hmm, I keep
using this same sequence of 3 or 4 operations. I guess I'll move them
out to a separate function and call it it in the appropriate places".

Just as some operations can't be factored out with simple function
calls, hence we have iterators and context managers, so not all
operations will be able to be factored out of a coroutine with "yield
from" (hence why I consider "yield" to be the more appropriate core
primitive, with "yield from" just correctly factoring out the task of
complete delegation, which is otherwise hard to do correctly)

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia



More information about the Python-ideas mailing list