Timers vs Coroutines

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
Post Reply
luigibyte
Posts: 4
Joined: Fri Sep 28, 2018 9:34 pm

Timers vs Coroutines

Post by luigibyte » Fri Sep 28, 2018 9:52 pm

Hi everyone,

I am new in the Micropython world and I have some doubts about what is the right way to do some stuff
I am working on a project to control several devices all around my house. I am using MQTT to perform communications.

My device is running Micropython on an ESP8266.

I need to parallelize jobs, and for that I am using Timers that runs every X seconds or milliseconds, it depends.

So, my questions is: Is that the right way to do it?. I have been investigating coroutines and I did some experiments but this coroutines have side effects.

For example:

I really like the ability to update the code of the device remotely using the webrepl and I don't want to lose that feature

When I use coroutines the webrepl gets blocked

What do you think guys? I would like to heard your recommendations

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Timers vs Coroutines

Post by pythoncoder » Sat Sep 29, 2018 7:29 am

The tutorial in this repo has detailed information on the use of coroutines for scheduling. In summary coroutines don't provide true concurrency. This makes using them much simpler than techniques which do, such as threads or interrupt handlers: there is less scope for seriously nasty concurrency bugs.

The fact that coroutines don't provide true concurrency has consequences. When one coroutine is running, no other coroutine is running. When a scheduler like uasyncio is running, no MicroPython background task (other than hardware interrupt handlers) will run.

I haven't tested the WebREPL with uasyncio; it doesn't surprise me that it won't work because an entire uasyncio application will hog the MicroPython VM just as much as

Code: Select all

while True:
    pass
I think you'd need to exit the application to re-enable access to the WebREPL.
Peter Hinch
Index to my micropython libraries.

luigibyte
Posts: 4
Joined: Fri Sep 28, 2018 9:34 pm

Re: Timers vs Coroutines

Post by luigibyte » Sat Sep 29, 2018 7:14 pm

Thank you pythoncoder,

I think I understand coroutines.
A possible solution to avoid blocking webrepl is to make it run as a coroutine, I found a library that changes the webrepl to be able to run inside a coroutine but I am not 100% confident.

What do you think about Timers? I am using them to parallelize work.

This is an snipped of my code:

[code]
from machine import Timer
t1 = Timer(-1)
t2 = Timer(-2)
def task_1():
# code here ...
def task_2():
# code here ...
t1.init(period=1000, mode=Timer.PERIODIC, callback=task_1)
t2.init(period=500, mode=Timer.PERIODIC, callback=task_2)
[/code]

thanks in advance

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Timers vs Coroutines

Post by pythoncoder » Sun Sep 30, 2018 11:28 am

I suggest you read this doc on the subject of writing interrupt handlers. There are a number of tricky issues which can occur with pre-emptive scheduling.
Peter Hinch
Index to my micropython libraries.

luigibyte
Posts: 4
Joined: Fri Sep 28, 2018 9:34 pm

Re: Timers vs Coroutines

Post by luigibyte » Sun Sep 30, 2018 11:10 pm

thank you Peter, I have already read it

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Timers vs Coroutines

Post by pythoncoder » Mon Oct 01, 2018 6:35 am

If you've read the docs I've linked to, you will be aware of the issues. To summarise, using timers to achieve preemptive scheduling is feasible, but preemptive solutions require very careful programming.
Peter Hinch
Index to my micropython libraries.

Post Reply