-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstoppable_thread.py
69 lines (55 loc) · 2.14 KB
/
stoppable_thread.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
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
import threading
import time
class StopableThreadMixing(threading.Thread):
def __init__(self, loop_time, sleeping_loop=0.01):
self.loop_time = loop_time
self.action_time = loop_time / sleeping_loop
self.sleeping_loop = sleeping_loop
threading.Thread.__init__(self)
def start(self, ):
self.running = True
self.time = 0
threading.Thread.start(self)
def loop_run_seconds(self):
return (self.time%self.action_time) * self.sleeping_loop
def _increment_time(self):
self.time+=1
self.time=self.time%self.action_time
def run(self):
while self.running:
if self.time == 0:
self.Action()
self._increment_time()
time.sleep(self.sleeping_loop)
def stop(self):
print 'stop'
self.running = False
self.join()
self.Cleanup()
class BackgroundRecording(StopableThreadMixing):
name = "A"
current_recording = None
record_list = []
def __init__(self, service, event_duration_sec=60, event_duration_min=None,*argv, **kwarg):
if event_duration_min: event_duration_sec = event_duration_min * 60.0
print "== Event Duration is %dsec==" % event_duration_sec
StopableThreadMixing.__init__(self, event_duration_sec, *argv, **kwarg)
self.service = service
def Action(self):
if self.current_recording:
print "== Stop Record: %s ==" % self.current_recording
self.current_recording = "Event%s" % self.name
self.record_list.append(self.current_recording)
self.name = chr (ord(self.name) + 1)
print "== Start Record:%s ==" % self.current_recording
def Cleanup(self):
print 'cleanup'
if self.current_recording:
print "== Stop Record: %s ==" % self.current_recording
print self.record_list
print self.loop_run_seconds()
if __name__ == "__main__":
t = BackgroundRecording("MSL.MSL_MASTER_SRV_19", event_duration_min=0.25)
t.start()
time.sleep(45)
t.stop()