Welcome, guest | Sign In | My Account | Store | Cart

Starts a timer that can be stopped or acts upon time-out.

Python, 104 lines
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
"""
  This module is helpful when something must be done on a timer.
  It is for testing purposes but the main class can be used in other applications
  with modifications to the timerExpired function. You can put your own stuff in there
  that must happen when the timer has expired.
  
  It is used by executing it from the shell with arguments -t, -s and optional -x.
  argument -t: timer duration in seconds.
  argument -s: choice to stop the timer after a default 5 seconds,
  optional -x: if -s was 'y' and an delay in seconds before the timer must be stopped.
  
  
  Author: Johan Geldenhuys
          AccessTel
  Date  : 2005-08-11
  
"""

# The threading module is used that has the code to start
# and stop the timer instance, so, why not use it?
from threading import Timer

# time module is imported to sleep a while after timer is started 
# before stopping it.
import time

# Main class to get timer instance going
class timer:
      
      # start timer with n seconds and go to self.timerExpired
      def startTimer(self, n):
        self.t = Timer(n, self.timerExpired)
        print 'Timer started for %d seconds'% n
        
        #start the timer instance
        self.t.start()
        
      # Called if argument is true to stop it.
      def stopTimer(self):
        
        # First look if the timer is active
        if self.t.isAlive():
                print 'Timer found active'
                
                #Stop the timer instance
                self.t.cancel()
                print 'Timer stopped'
        
        # If not active, do nothing
        else:
                print 'Timer inactive'
                
      # Put code in here to execute after timer has expired.
      def timerExpired(self):
        print 'Timer expired, go to do what you want'
        
# Help the user understand how to use this module
def printUsageAndExit():
    print
    print "Usage: %s -t[time-out] "\
          "-s[sleep before stopping, 'y' or 'n'] \r\noptional -x[seconds to wait before stopping]\r\n\r\n"\
          "Example: timertest.py -t 10 -s y -x 6\r\n" % sys.argv[0]
    print
    sys.exit(1)

    
if __name__ == '__main__':
        
        import sys, getopt, string
        
        l = (len(sys.argv))
        
        if l <= 4:
           printUsageAndExit()
        arg3 = int('5')
        optlist, args = getopt.getopt(sys.argv[1:], "t:s:x:")
        for opt in optlist:
            
            if opt[0] == '-t':
               if not (opt[1].isdigit()):
                  printUsageAndExit()
               arg1 = int(opt[1])
               
            elif opt[0] == '-s':
               arg2 = (opt[1])
                  
               #printUsageAndExit()
               
               
            elif opt[0] == '-x':
               if not (opt[1].isdigit()):
                  printUsageAndExit()
               arg3 = int(opt[1])
               
        
        x = timer()
        if arg2 == 'y':
           x.startTimer(arg1)
           time.sleep(arg3)
           print 'You chose to stop the timer after %s with option %s'%(str(arg3), arg2)
           x.stopTimer()
           
        if arg2 == 'n':
           x.startTimer(arg1)
           

This module can be used for something that must happen when the timer expires. Very simply starts a timer that can be stopped or if not stopped will expire after (n) seconds. Can be called from the command line or the class can be used in other modules and called from there with minor changes.