Green threads on pyboard

Some weeks ago i received new fancy device — pyboard. It’s like arduino, but using python instead of wiring. I was little disappointed, because micropython wasn’t have multithreading module.

And i developed microasync — library with green threads for micropython.

Little example, code for toggling leds concurrently:

from microasync.async import loop, coroutine, Delay
import pyb

@coroutine  # decorator for making green thread from function
def toggle_led_on_interval(led, interval):
    while True:
        yield Delay(interval)  # like time.sleep, but non-blocking

toggle_led_on_interval(1, 1)
toggle_led_on_interval(2, 2)
toggle_led_on_interval(3, 1)
toggle_led_on_interval(4, 2)

# start main loop:

And result:

comments powered by Disqus