Page 2 of 12

Re: asyncio-like cooperative multitasking framework for uPy

Posted: Mon Oct 20, 2014 10:40 pm
by pfalcon
Ok, asyncio branch was finally merged into micropython-lib master. This means it's little bit more mature than it was before ;-). There're still changes to do to optimize it for running on pyboard, and to document it.

In the meantime, I started to write HTTP client for it: https://github.com/pfalcon/micropython-uaiohttpclient

Re: asyncio-like cooperative multitasking framework for uPy

Posted: Fri Oct 24, 2014 9:02 pm
by pfalcon
uasyncio was split into 2 packages: uasyncio.core and uasyncio. uasyncio.core contains basic event loop implementation and should work on bare-metal targets like PyBoard. uasyncio adds POSIX (well, Linux) async I/O handling on top of that.

Re: asyncio-like cooperative multitasking framework for uPy

Posted: Fri Oct 31, 2014 10:06 am
by fma
Last night, I made a short talk to present micropython to AFPY guys, and someone also presented asyncio.

At the end of the talk, we tried to use uasyncio to blink leds, but it failed. It seems that asyncio can't work on pyboard, because the ffi module is missing.

Am I right?

Is it possible to only use usayncio.core on pyboard to make a basic asyncio demo? How do I use it?

Thanks for your help.

Re: asyncio-like cooperative multitasking framework for uPy

Posted: Sun Nov 02, 2014 6:13 pm
by pfalcon
fma wrote:Last night, I made a short talk to present micropython to AFPY guys, and someone also presented asyncio.
Great!
At the end of the talk, we tried to use uasyncio to blink leds, but it failed. It seems that asyncio can't work on pyboard, because the ffi module is missing.

Am I right?

Is it possible to only use usayncio.core on pyboard to make a basic asyncio demo?
Yes, as the previous message says, uasyncio.core was specifically split to let it being used on PyBoard. Full uasyncio module cannot be used on PyBoard, because it used Linux async i/o implementation (epoll). Surely, that will change with time.
How do I use it?

Thanks for your help.
I don't have a PyBoard with me, so don't to post unverified code, so here's code which will "blink" ON/OFF messages on your desktop monitor:

Code: Select all

import logging
try:
    import uasyncio.core as asyncio
except ImportError:
    import asyncio


def loop():
    while True:
        print("ON")
        yield from asyncio.sleep(1)
        print("OFF")
        yield from asyncio.sleep(1)

logging.basicConfig(level=logging.ERROR)
asyncio.get_event_loop().run_until_complete(loop())
This code will work: 1) on python3.4+ on desktop; 2) on micropython on desktop; 3) on micropython on PyBoard. For the latter case, you can replace print's with corresponding Pin operations to get LED blinking. This example also shows that at the basic level, CPython's asyncio and MicroPython's uasyncio are compatible. There're incompatibilities if you go a bit further, but I have ideas how to improve it too, on TODO.

Re: asyncio-like cooperative multitasking framework for uPy

Posted: Sun Nov 02, 2014 7:02 pm
by fma
Thanks! I'll test that.

Re: asyncio-like cooperative multitasking framework for uPy

Posted: Mon Nov 03, 2014 7:59 am
by fma
Ok, I wrote this little code:

Code: Select all

import logging
import uasyncio.core as asyncio
import pyb


@asyncio.coroutine
def light(led, delayOn, delayOff):
    """
    """
    while True:
        print(repr(led))
        led.on()
        yield from asyncio.sleep(delayOn)
        led.off()
        yield from asyncio.sleep(delayOff)


def main():
    """
    """
    logging.basicConfig(level=logging.ERROR)

    loop = asyncio.get_event_loop()

    leds = [pyb.LED(1), pyb.LED(2), pyb.LED(3), pyb.LED(4)]
    #delays = [(1, 2), (1, 0.5), (0.5, 0.5), (0.25, 1)]
    delays = [(1, 1), (1, 2), (2, 1), (2, 2)]
    tasks = [asyncio.async(light(led, *delay)) for led, delay in zip(leds, delays)]

#    loop.run_until_complete(asyncio.wait(tasks))  # not implemented
    for task in tasks:
        loop.call_soon(task)
    loop.run_forever()


if __name__ == "__main__":
    main()
It works fine if I use intergers as delays, but not if I use floats. Any idea why?

Re: asyncio-like cooperative multitasking framework for uPy

Posted: Mon Nov 03, 2014 11:19 am
by pfalcon
If you look at the source, timing is handled via EventLoop.time(), which in turn uses time.time(). Whatever your platform's time.time() provides, that you have. Unix port provides fractional time values. STMHal port has only single-precision floats, so cannot represent both calendar time and subsecond precision in one value, so that's what you get. You can subclass EventLoop and override its time() to use whatever STMHal port provides of needed precision.

Re: asyncio-like cooperative multitasking framework for uPy

Posted: Mon Nov 03, 2014 2:57 pm
by fma
Thanks!

Re: asyncio-like cooperative multitasking framework for uPy

Posted: Tue Nov 04, 2014 7:57 am
by fma
Is it possible to use pyb.millis()? Or will it stop working after 12.4 days?

Re: asyncio-like cooperative multitasking framework for uPy

Posted: Tue Nov 04, 2014 9:16 am
by dhylands
There is a pyb.elapsedMillis() that takes rollovers into consideration.
http://docs.micropython.org/en/latest/l ... sed_millis