Question

I need a function similar to gobject.io_add_watch but for a variable. For example it needs to watch the variable stop initialized to stop = False and when stop is changed to True it must call a function. I can't have a separate thread watching the variable in a loop with a time.sleep.

Is there such a function or a way to do that ?

Was it helpful?

Solution

Use a property in a class:

class Stopwatch(object):
    def __init__(self, callback):
        self._stop = False
        self.callback = callback

    @property
    def stop(self): return self._stop

    @stop.setter
    def stop(self, value):
        self._stop = value
        if value: self.callback()
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top