0

I have copied the code from this webaddress http://code.activestate.com/recipes/577407-resettable-timer-class-a-little-enhancement-from-p/

To create a resettable timer, and it works nicely but once it has finished it ends. I would like to start it again but I'm new and couldn't work out how to do it.

I tired creating a new instance of itself at the bottom of the run() function after the line:

print "Time: %s - timer finished!" % time.asctime()

but python didn't seem to like it.

The copy pasted code created by Eddy Jacob is this :

from threading import Thread, Event, Timer
import time

def TimerReset(*args, **kwargs):
    """ Global function for Timer """
    return _TimerReset(*args, **kwargs)


class _TimerReset(Thread):
    """Call a function after a specified number of seconds:

    t = TimerReset(30.0, f, args=[], kwargs={})
    t.start()
    t.cancel() # stop the timer's action if it's still waiting
    """

    def __init__(self, interval, function, args=[], kwargs={}):
        Thread.__init__(self)
        self.interval = interval
        self.function = function
        self.args = args
        self.kwargs = kwargs
        self.finished = Event()
        self.resetted = True

    def cancel(self):
        """Stop the timer if it hasn't finished yet"""
        self.finished.set()

    def run(self):
        print "Time: %s - timer running..." % time.asctime()

        while self.resetted:
            print "Time: %s - timer waiting for timeout in %.2f..." % (time.asctime(), self.interval)
            self.resetted = False
            self.finished.wait(self.interval)

        if not self.finished.isSet():
            self.function(*self.args, **self.kwargs)
        self.finished.set()
        print "Time: %s - timer finished!" % time.asctime()

    def reset(self, interval=None):
        """ Reset the timer """

        if interval:
            print "Time: %s - timer resetting to %.2f..." % (time.asctime(), interval)
            self.interval = interval
        else:
            print "Time: %s - timer resetting..." % time.asctime()

        self.resetted = True
        self.finished.set()
        self.finished.clear()

I ran my timer using :

tim = TimerReset(10, AddBlank)
tim.start()

The function AddBlank does something but I would like the timer to be called again but only after it has ended. Really appreciate help with this, I know copying code without understanding it is bad practice but I really just wanted a simple resettable timer to call a function repeatedly unless reset and this almost does that.

1 Answer 1

1

Change the run function to:

def run(self):
    while not self.finished.isSet():
        print "Time: %s - timer running..." % time.asctime()

        self.resetted = True
        while self.resetted:
            print "Time: %s - timer waiting for timeout in %.2f..." % (time.asctime(), self.interval)
            self.resetted = False
            self.finished.wait(self.interval)

        if not self.finished.isSet():
            self.function(*self.args, **self.kwargs)
    print "Time: %s - timer finished!" % time.asctime()

This will cause it to run indefinitely until finished is set.

Sign up to request clarification or add additional context in comments.

2 Comments

This is good, alas the python crashed when 'unloading'/terminating the program.
Wish I could have helped more. I'm pretty sure the problem lies in properly ending the thread upon unloading, but I saw (in chat) you couldn't get that to work. If you add more details (or open a new question concerning that part) I or someone else may be able to help. :3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.