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]

Reply via email to