-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
39 lines (34 loc) · 985 Bytes
/
test.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
import threading
import time
class RIPTimer(threading.Thread):
'''
create a thread object that will do the counting in the background
default interval is 1/1000 of a second
'''
def __init__(self, interval=1):
# init the thread
threading.Thread.__init__(self)
self.interval = interval # seconds
# initial value
self.value = 0
# controls the while loop in method run
self.alive = False
def run(self):
'''
this will run in its own thread via self.start()
'''
self.alive = True
while self.alive:
time.sleep(self.interval)
# update count value
self.value += self.interval
print(self.value)
def finish(self):
'''
close the thread, return final value
'''
# stop the while loop in method run
self.alive = False
return self.value
thread1 = RIPTimer(1)
thread1.start()