-
Notifications
You must be signed in to change notification settings - Fork 0
/
starvation.py
56 lines (40 loc) · 1.1 KB
/
starvation.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
import enum
import queue
import threading
class command (enum.Enum):
START = 0
STOP = 1
END = 2
def check_thread (wait, aqueue, func):
while True:
value = aqueue.get()
amount = 0
while value is command.START:
try:
value = aqueue.get(timeout=wait)
except queue.Empty:
func(amount)
amount += 1
if value is command.END:
break
class checker (object):
__slots__ = [ "thread", "aqueue" ]
def __init__ (self, func, wait):
self.aqueue = queue.Queue()
self.thread = threading.Thread(target=check_thread, kwargs={
"wait": wait, "aqueue": self.aqueue, "func": func
})
self.thread.start()
def check (self):
self.aqueue.put(command.START)
def uncheck (self):
self.aqueue.put(command.STOP)
def end (self):
self.aqueue.put(command.END)
self.thread.join()
def __enter__ (self, *_):
self.check()
def __exit__ (self, *_):
self.uncheck()
def __del__ (self):
self.end()