-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtimer.py
34 lines (29 loc) · 879 Bytes
/
timer.py
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
#!/usr/bin/env python
#encoding: utf-8
import threading,time
class ATimer(threading.Thread):
def __init__(self,fn,args=(),sleep=1,lastDo=True):
threading.Thread.__init__(self)
self.fn = fn
self.args = args
self.sleep = sleep
self.lastDo = lastDo
self.setDaemon(True)
self.isPlay = True
self.fnPlay = False
def __do(self):
self.fnPlay = True
apply(self.fn,self.args)
self.fnPlay = False
def run(self):
while self.isPlay :
self.__do()
time.sleep(self.sleep)
def stop(self):
#stop the loop
self.isPlay = False
while True:
if not self.fnPlay : break
time.sleep(0.01)
#if lastDo,do it again
if self.lastDo : self.__do()