Hello,

On Wed, 12 Nov 2014 20:52:07 -0800
Guido van Rossum <[email protected]> wrote:

> No.
> 
> The reason is that "yield <coroutine>" is intentionally an error,
> because it is likely a misspelling for "yield from <coroutine>" --
> which means something completely different from what you are
> proposing.

Well, there definitely *can* be confusion, as Victor's reply shows
(well, maybe I'm missing something from his reply too ;-) ). But let's
be fair - the ability to use asyncio at all, and efficiently in
particular, depends on understanding of differences between "yield",
"yield from", and normal call operator. And there're bigger confusions
than "yield" vs "yield from".

For example, the biggest issue I've personally hit so far is forgetting
to use "yield from" at all. So, I write "stream.read()", and then
wonder why nothing works. I do that on MicroPython/uasyncio of course,
and in such mediated way understand why there's @asyncio.coroutine
decorator and that envvar which, being set, dumps non-iterated
coroutines at the program termination.

But I wouldn't consciously confuse "yield" and "yield from" - they are
rather different, each serving its own purpose. Maybe that's because
I spent quite some time implementing "yield from" in MicroPython, so
had to understand it well enough ;-).

Speaking of which, to help with my understanding on "yield from" at
that time, I drew a diagram, kind of infographic. I wanted to elaborate
it beyond "a personal note", but never got to it, so I wonder if I
should share it is as after all, maybe some Python developers will find
it funny: https://dl.dropboxusercontent.com/u/44884329/yield-from.pdf


> 
> On Wed, Nov 12, 2014 at 8:32 PM, Paul Sokolovsky <[email protected]>
> wrote:
> 
> > Hello,
> >
> > On Thu, 13 Nov 2014 01:38:53 +0100
> > Victor Stinner <[email protected]> wrote:
> >
> > > 2014-11-13 0:18 GMT+01:00 Paul Sokolovsky <[email protected]>:
> > > > (...) do you think that it might be possible to go step
> > > > further and allow scheduling a new coroutine by just yielding a
> > > > generator instance? In other words:
> > >
> > > (I don't see how it is related to create_task. create_task is
> > > nothing new, it's just an helper to not have to pass the loop as
> > > a parameter.)
> >
> > Why, directly related. The talk is about replacing
> > "loop.create_task(coro)" with "yield coro". "yield from" is not
> > related in any way. In the case of "yield coro", the loop targeted
> > is implicitly the current running loop, just the same as "yield
> > from asyncio.sleep()", etc. Of course, yield can be used only in
> > coroutines, so doesn't replace .create_task() completely, which
> > still may be needed in "bootstrap" code.
> >
> >
> > Here're are examples:
> >
> > Before:
> >
> > ==========
> > try:
> >     import uasyncio.core as asyncio
> > except:
> >     import asyncio
> >
> > def do_little():
> >     for i in range(3):
> >         print(i)
> >         yield
> >
> > def main():
> >     for i in range(5):
> >         loop.create_task(do_little())
> >         yield
> >
> > import logging
> > logging.basicConfig(level=logging.INFO)
> > loop = asyncio.get_event_loop()
> > loop.create_task(main())
> > loop.run_forever()
> > ==========
> >
> > after:
> >
> > ==========
> > try:
> >     import uasyncio.core as asyncio
> > except:
> >     import asyncio
> >
> > def do_little():
> >     for i in range(3):
> >         print(i)
> >         yield
> >
> > def main():
> >     for i in range(5):
> >         yield do_little()
> >
> > import logging
> > logging.basicConfig(level=logging.INFO)
> > loop = asyncio.get_event_loop()
> > loop.create_task(main())
> > loop.run_forever()
> > ==========
> >
> >
> > Excuse the boilerplate code, tested to run on MicroPython too. The
> > diff is:
> >
> > -        loop.create_task(do_little())
> > -        yield
> > +        yield do_little()
> >
> >
> > --
> > Best regards,
> >  Paul                          mailto:[email protected]
> >
> 
> 
> 
> -- 
> --Guido van Rossum (python.org/~guido)



-- 
Best regards,
 Paul                          mailto:[email protected]

Reply via email to