Page 1 of 1

Timers vs Coroutines

Posted: Fri Sep 28, 2018 9:52 pm
by luigibyte
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

Re: Timers vs Coroutines

Posted: Sat Sep 29, 2018 7:29 am
by pythoncoder
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.

Re: Timers vs Coroutines

Posted: Sat Sep 29, 2018 7:14 pm
by luigibyte
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

Re: Timers vs Coroutines

Posted: Sun Sep 30, 2018 11:28 am
by pythoncoder
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.

Re: Timers vs Coroutines

Posted: Sun Sep 30, 2018 11:10 pm
by luigibyte
thank you Peter, I have already read it

Re: Timers vs Coroutines

Posted: Mon Oct 01, 2018 6:35 am
by pythoncoder
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.