El modulo customTimer.py es una clase de ayuda que encontre en la web ya que no encontraba respuesta alguna asi que este es el codigo:
Código Python:
Ver originalimport threading
import time
TIMER_ON_TIME = 1
class ResettableTimer(threading.Thread):
"""
The ResettableTimer class is a timer whose counting loop can be reset
arbitrarily. Its duration is configurable. Commands can be specified
for both expiration and update. Its update resolution can also be
specified. Resettable timer keeps counting until the "run" method
is explicitly killed with the "kill" method.
"""
def __init__(self, maxtime, expire, inc=None, update=None):
"""
@param maxtime: time in seconds before expiration after resetting
in seconds
@param expire: function called when timer expires
@param inc: amount by which timer increments before
updating in seconds, default is maxtime/2
@param update: function called when timer updates
"""
self.maxtime = maxtime
self.expire = expire
if inc:
self.inc = inc
else:
self.inc = maxtime/2
if update:
self.update = update
else:
self.update = lambda c : None
self.counter = 0
self.active = True
self.stop = False
threading.Thread.__init__(self)
self.setDaemon(True)
def set_counter(self, t):
"""
Set self.counter to t.
@param t: new counter value
"""
self.counter = t
def deactivate(self):
"""
Set self.active to False.
"""
self.active = False
def kill(self):
"""
Will stop the counting loop before next update.
"""
self.stop = True
def reset(self):
"""
Fully rewinds the timer and makes the timer active, such that
the expire and update commands will be called when appropriate.
"""
self.counter = 0
self.active = True
def run(self):
"""
Run the timer loop.
"""
while True:
self.counter = 0
while self.counter < self.maxtime:
self.counter += self.inc
time.sleep(self.inc)
if self.stop:
return
if self.active:
self.update(self.counter)
if self.active:
self.active = False
self.expire()
class Relay:
def __init__(self, id):
self.id = id
print '** Starting ' + str(id) + ' ON for ' + str(TIMER_ON_TIME) + ' seconds'
self.timer = ResettableTimer(TIMER_ON_TIME, self.process_event)
self.timer.start()
# handle the relay switching on timer event
def process_event(self):
# execute some logic
# ...
# ...
print 'Inside event for Relay: ', str(self.id)
# reset the timer
self.timer.maxtime = TIMER_ON_TIME
# restart the timer
print '@ Restarting timer: ', str(self.id)
self.timer.reset()
########### MAIN ###########
# create 3 timer objects
relays = []
for i in range( 3 ):
relays.append( Relay( i ) )
# Main loop
while True:
time.sleep( 10 ) # sleep until it's time to do something in this loop
print '>> active threads: ', threading.activeCount(), '\n' # this might need to be active_count()
# depending on your python version