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
   | import csv
import threading
import time
 
 
class MyTimer:
    def __init__(self, tempo, target, args= [], kwargs={}):
        self._target = target
        self._args = args
        self._kwargs = kwargs
        self._tempo = tempo
        self.record_data  = []
        self.state  = "INIT"
 
 
    def _run(self):
        self._timer = threading.Timer(self._tempo, self._run)
        self._timer.start()
        self.state = self._target(self.state,*self._args, **self._kwargs)
 
    def start(self):
        self._timer = threading.Timer(self._tempo, self._run)
        self._timer.start()
 
    def stop(self):
        self._timer.cancel()
 
    def display(self):
        print self.record_data
 
 
 
def calcul(state,x,y):
 
    if state == "INIT":
        result = x+y+8
        state  = "RUN"
    else:
        result = x+y+7
    print result
    return state
 
 
 
 
 
 
 
## main
 
Te=0.05
input = 2
a = MyTimer(Te,calcul,(2,3))
 
 
a.start()
time.sleep(2)
print "Timer arrete"
print a.display():D
a.stop()
time.sleep(2.0) | 
Partager