asyncio-like cooperative multitasking framework for uPy

Discussion about programs, libraries and tools that work with MicroPython. Mostly these are provided by a third party.
Target audience: All users and developers of MicroPython.
pfalcon
Posts: 968
Joined: Fri Feb 28, 2014 2:05 pm

Re: asyncio-like cooperative multitasking framework for uPy

Post by pfalcon » Mon Oct 20, 2014 10:40 pm

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

pfalcon
Posts: 968
Joined: Fri Feb 28, 2014 2:05 pm

Re: asyncio-like cooperative multitasking framework for uPy

Post by pfalcon » Fri Oct 24, 2014 9:02 pm

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.

User avatar
fma
Posts: 156
Joined: Wed Jan 01, 2014 5:38 pm
Location: France

Re: asyncio-like cooperative multitasking framework for uPy

Post by fma » Fri Oct 31, 2014 10:06 am

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.
Frédéric

pfalcon
Posts: 968
Joined: Fri Feb 28, 2014 2:05 pm

Re: asyncio-like cooperative multitasking framework for uPy

Post by pfalcon » Sun Nov 02, 2014 6:13 pm

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.

User avatar
fma
Posts: 156
Joined: Wed Jan 01, 2014 5:38 pm
Location: France

Re: asyncio-like cooperative multitasking framework for uPy

Post by fma » Sun Nov 02, 2014 7:02 pm

Thanks! I'll test that.
Frédéric

User avatar
fma
Posts: 156
Joined: Wed Jan 01, 2014 5:38 pm
Location: France

Re: asyncio-like cooperative multitasking framework for uPy

Post by fma » Mon Nov 03, 2014 7:59 am

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?
Frédéric

pfalcon
Posts: 968
Joined: Fri Feb 28, 2014 2:05 pm

Re: asyncio-like cooperative multitasking framework for uPy

Post by pfalcon » Mon Nov 03, 2014 11:19 am

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.

User avatar
fma
Posts: 156
Joined: Wed Jan 01, 2014 5:38 pm
Location: France

Re: asyncio-like cooperative multitasking framework for uPy

Post by fma » Mon Nov 03, 2014 2:57 pm

Thanks!
Frédéric

User avatar
fma
Posts: 156
Joined: Wed Jan 01, 2014 5:38 pm
Location: France

Re: asyncio-like cooperative multitasking framework for uPy

Post by fma » Tue Nov 04, 2014 7:57 am

Is it possible to use pyb.millis()? Or will it stop working after 12.4 days?
Frédéric

User avatar
dhylands
Posts: 2494
Joined: Mon Jan 06, 2014 6:08 pm
Location: Shuswap, BC, Canada
Contact:

Re: asyncio-like cooperative multitasking framework for uPy

Post by dhylands » Tue Nov 04, 2014 9:16 am

There is a pyb.elapsedMillis() that takes rollovers into consideration.
http://docs.micropython.org/en/latest/l ... sed_millis

Post Reply