[Python-ideas] yield from multiple iterables (was Re: The async API of the future: yield-from)
Guido van Rossum
guido at python.org
Tue Oct 23 16:44:31 CEST 2012
On Tue, Oct 23, 2012 at 12:17 AM, Jim Jewett <jimjjewett at gmail.com> wrote:
> On 10/19/12, Guido van Rossum <guido at python.org> wrote:
>
>> I did a basic timing test using a simple recursive function and a
>> recursive PEP-380 coroutine computing the same value (see attachment).
>> The coroutine version is a little over twice as slow as the function
>> version. I find that acceptable. This went 20 deep, making 2 recursive
>> calls at each level (except at the deepest level).
>
> Note that the co-routine code (copied below) does not involve a
> scheduler that unwraps futures; there is no scheduler, and nothing
> runs concurrently.
>
> def coroutine(n):
> if n <= 0:
> return 1
> l = yield from coroutine(n-1)
> r = yield from coroutine(n-1)
> return l + 1 + r
>
> I like the above code; my concern was that yield might get co-opted
> for use with scheduler loops, which would have to track the parent
> task explicitly, and prevent it from being rescheduled too early.
Don't worry. There is no way that a scheduler can change the meaning
of yield from. All its power stems from its ability to decide when to
call next(), and that is the same power that the app has itself.
--
--Guido van Rossum (python.org/~guido)
More information about the Python-ideas
mailing list