[Python-ideas] The async API of the future: yield-from
Greg Ewing
greg.ewing at canterbury.ac.nz
Tue Oct 16 22:27:53 CEST 2012
Nick Coghlan wrote:
> # Note that this is an *ordinary iterator*, not a tasklet
> def as_completed(futures):
> # We ensure all the operations have started, and get ourselves
> a set to work with
> remaining = set(futures)
> while remaining:
> # The trick here is that we *don't yield the original
> futures directly*
> # Instead, we yield
> yield _wait_first(remaining)
I've just figured out how your as_completed() thing works,
and realised that it's *not* a general solution to the
suspendable-iterator problem. You're making use of the fact
that you know *how many* values there will be ahead of time,
even if you don't know what they are yet.
In general this won't be the case. I don't think there is
any trick that will allow a for-loop to be used in the general
case, because in order for an iterator to be suspendable, the
call to next() would need to be made using yield-from, and
it's hidden inside the for-loop implementation.
I know you probably weren't intending as_completed() to be
a solution to the general suspendable-iterator problem.
I just wanted to record my thoughts on this.
--
Greg
More information about the Python-ideas
mailing list