-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstopth.py
27 lines (25 loc) · 883 Bytes
/
stopth.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
"""
code from https://stackoverflow.com/questions/323972/is-there-any-way-to-kill-a-thread
and https://stackoverflow.com/questions/36173410/python-terminate-a-thread-when-it-is-sleeping
"""
import threading
class StoppableThread(threading.Thread):
def __init__(self,target,args=(),timeout=15):
super(StoppableThread,self).__init__()
self._target=target
self._timeout=timeout
self._args=args
self._stop=threading.Event()
def setTimeOut(self,timeOut):
self._timeout=timeOut
def setArgs(self,args):
self._args=args
def run(self):
while not self.stopped():
if not self.stopped():
self._target(*self._args)
self._stop.wait(self._timeout)
def stop(self):
self._stop.set()
def stopped(self):
return self._stop.is_set()