WDT feeding in the background, is this a good idea?


  • Pybytes Beta

    I want to keep my WDT fed but I would rather not write wdt.feed() all over the place. Is the code below a good idea or will it negate the purpose of having a WDT?

    My gut tells me this is wrong but I don't know enough to be sure.

    class WDT(object):
        def __init__(self, timeout = 10000):
            from machine import WDT
            self.wdt = WDT(timeout)
    
    
        def feeder_func(delay, id):
            from time import sleep
            while True:
                sleep(delay)
                self.wdt.feed()
    
    
        # TODO How do you kill a thread?
        def start()
            import _thread
            _thread.start_new_thread(feeder_func, 0)
    
    
        def stop(self):
            self.wdt.stop()
    

    Usage:
    >>> wdt = WDT()
    >>> wdt.start() # Being fed in the background...
    >>> wdt.stop()


  • Pybytes Beta

    I will stop the WDT just before deep sleep, and waking from sleep requires a reset, so it's not looped for me. main.py can take anywhere from a few seconds to several minutes to execute, so I don't know the maximum duration ahead of time.

    It would seem I would need to feed it all over the place, so I'll just fire up my copy and pasting.



  • @BetterAuto If you use deepsleep that is also some kind of loop. The way I use the WDT is to check if it can finish the loop, so 1 feed at the end. This assumes you somewhat know a maximum duration the loop can take. So I would just put it before putting the device to sleep. That way, if it hangs it will re-run the program until it succeeds.


  • Pybytes Beta

    I don't have a main loop. I have a main.py that executes once then the board goes back to sleep. So this would catch what you believe is the most common failure, that's good.

    Is there a guideline for how often to slip your feeders into the code? Before every statement? Every tenth statement? Only before expensive operations? Something else?



  • Why not just put it once in your main loop?
    Anyway, I've notifced crashes/freezes happen due to memory errors (if the rest of your code is well written). So the thread will also stop working when this happens, and the board will reset.


Log in to reply
 

Pycom on Twitter