Ver Mensaje Individual
  #3 (permalink)  
Antiguo 18/05/2012, 10:02
xogost
 
Fecha de Ingreso: mayo-2011
Mensajes: 4
Antigüedad: 13 años
Puntos: 1
Respuesta: Timer en Python

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 original
  1. import threading
  2. import time
  3.  
  4. TIMER_ON_TIME = 1
  5.  
  6. class ResettableTimer(threading.Thread):
  7.   """
  8.  The ResettableTimer class is a timer whose counting loop can be reset
  9.  arbitrarily. Its duration is configurable. Commands can be specified
  10.  for both expiration and update. Its update resolution can also be
  11.  specified. Resettable timer keeps counting until the "run" method
  12.  is explicitly killed with the "kill" method.
  13.  """
  14.   def __init__(self, maxtime, expire, inc=None, update=None):
  15.     """
  16.    @param maxtime: time in seconds before expiration after resetting
  17.                    in seconds
  18.    @param expire: function called when timer expires
  19.    @param inc: amount by which timer increments before
  20.                updating in seconds, default is maxtime/2
  21.    @param update: function called when timer updates
  22.    """
  23.     self.maxtime = maxtime
  24.     self.expire = expire
  25.     if inc:
  26.       self.inc = inc
  27.     else:
  28.       self.inc = maxtime/2
  29.     if update:
  30.       self.update = update
  31.     else:
  32.       self.update = lambda c : None
  33.     self.counter = 0
  34.     self.active = True
  35.     self.stop = False
  36.     threading.Thread.__init__(self)
  37.     self.setDaemon(True)
  38.   def set_counter(self, t):
  39.     """
  40.    Set self.counter to t.
  41.  
  42.    @param t: new counter value
  43.    """
  44.     self.counter = t
  45.   def deactivate(self):
  46.     """
  47.    Set self.active to False.
  48.    """
  49.     self.active = False
  50.   def kill(self):
  51.     """
  52.    Will stop the counting loop before next update.
  53.    """
  54.     self.stop = True
  55.   def reset(self):
  56.     """
  57.    Fully rewinds the timer and makes the timer active, such that
  58.    the expire and update commands will be called when appropriate.
  59.    """
  60.     self.counter = 0
  61.     self.active = True
  62.  
  63.   def run(self):
  64.     """
  65.    Run the timer loop.
  66.    """
  67.     while True:
  68.       self.counter = 0
  69.       while self.counter < self.maxtime:
  70.         self.counter += self.inc
  71.         time.sleep(self.inc)
  72.         if self.stop:
  73.           return
  74.         if self.active:
  75.           self.update(self.counter)
  76.       if self.active:
  77.         self.active = False
  78.         self.expire()
  79.  
  80. class Relay:
  81.   def __init__(self, id):
  82.     self.id = id
  83.  
  84.     print '** Starting ' + str(id) + ' ON for ' + str(TIMER_ON_TIME) + ' seconds'
  85.  
  86.     self.timer = ResettableTimer(TIMER_ON_TIME, self.process_event)
  87.     self.timer.start()
  88.  
  89.   # handle the relay switching on timer event
  90.   def process_event(self):
  91.  
  92.     # execute some logic
  93.     # ...
  94.     # ...
  95.  
  96.     print 'Inside event for Relay: ', str(self.id)
  97.  
  98.     # reset the timer
  99.     self.timer.maxtime = TIMER_ON_TIME
  100.  
  101.     # restart the timer
  102.     print '@ Restarting timer: ', str(self.id)
  103.     self.timer.reset()
  104.  
  105. ########### MAIN ###########
  106.  
  107. # create 3 timer objects
  108. relays = []
  109. for i in range( 3 ):
  110.  
  111.   relays.append( Relay( i ) )
  112.  
  113. # Main loop
  114. while True:
  115.   time.sleep( 10 )  # sleep until it's time to do something in this loop
  116.   print '>> active threads: ', threading.activeCount(), '\n'  # this might need to be active_count()
  117.                                                                               # depending on your python version

Última edición por razpeitia; 18/05/2012 a las 23:23 Razón: Syntax Highlight!!!!