Wednesday, 15 April 2015

Resettable timer in python repeats until cancelled -


I have copied the code from this web address

to create a resetable timer, and this Works well but ends once it is finished. I would like to start it again, but I'm new and how to do it, it could not work.

I got tired of creating a new instance of the self in the lower part of the run () function after line:

  print "time:% s - the timer is over! " % Time.asctime ()   

But the dragon did not like it.

The code made by Eddie Jacob has been pasted:

  Threading import thread, event, timer import time DEF timer reset (* AGR, ** quad) : "" Global Function for Timer "" "Returns _TimerReset (* Args, ** Quarges) Class _TimerReset (Thread): Call a function after the specified seconds: T = TimerSet (30.0, F, Argos = [ ], Quad = {}) t. START () T.CANEL () # Stop the action of timer if it is still waiting for "def" __init __ (self, interval, work, logic = [], kwargs = {}): thread .__ init __ ( Self). Self.interval = interval self.function = function self.args = args self.kwargs = Kwargs self.finished = Event () self.resetted = Cancel true def (self): "" "If it is not finished Turn off the timer "" self.finished.set () def run (self): Print "Time:% s - Running timer ..."% time.asctime () while auto-resetting: Print " Time:% s - Waiting for timer in% .2f for timeout ("Self."): Self (self.interval) Self-dependent (): Self. Function (* self.args, ** self.kwargs) self.finished.set () Print "Time:% s - The timer is over! "% Time.asctime () def reset (self, interval = none):" "" reset timer "" if interval: print "time:% s - reset timer% .2f ..."% (time. Asctime (), interval) self.interval = interval Other: Print "time:% s - timer reset ..."% time.asctime () self.resetted = True self.finished.set () self.finished.clear ( )   

I started using my timer:

  tim = timer reset (10, adblock) tim.start ()   

The AddBlank function does something, but I have to call the timer again, but it will end Only then will it be. In fact, appreciate this with help, I understand the duplicate code without understanding it, that it is bad behavior, but in reality I want to do a simple resetter to call the function repeatedly to do a reset And it almost does this.

run function:

   

This will be the reason for running indefinitely until this finished is set.

No comments:

Post a Comment