I am writing simple code that catch sensor data and activates lights. So if object disappeared from sensor lights should be turned on for N seconds. The code I have now is blocking, so if object appeared back into sensor I do not catch this because still in the loop waiting when timeout expired. Is there any way to exit from a loop if sensor event happened again, so I can reset timeout and start over again?

better to re-write the code so it is not blocking.

You could use imp.wakeup to schedule an event N seconds later that turns the light off. In the meanwhile the imp would be free to do what it wants.

imp.wakeup returns a handler that you can use to handle the wakeup event if you need to.

The callback functions queue if I am not mistaken, so you could have them add to your timeout every time motion was detected.


Thanks guys, I re-wrote the code using imp.wakeup and imp.cancelwakeup functions to solve this. That was easier and better solution instead of having additional non-blocking loop.