StoppableThread sleep
This commit is contained in:
@ -21,7 +21,7 @@ class StoppableThread(threading.Thread):
|
|||||||
... self.cur = threading.current_thread()
|
... self.cur = threading.current_thread()
|
||||||
... self.E.set()
|
... self.E.set()
|
||||||
... while self.shouldRun():
|
... while self.shouldRun():
|
||||||
... time.sleep(0.01)
|
... self.sleep(10.0)
|
||||||
>>> T = TestThread()
|
>>> T = TestThread()
|
||||||
>>> T.start()
|
>>> T.start()
|
||||||
>>> T.E.wait(1.0)
|
>>> T.E.wait(1.0)
|
||||||
@ -34,24 +34,30 @@ class StoppableThread(threading.Thread):
|
|||||||
"""
|
"""
|
||||||
def __init__(self, max=0):
|
def __init__(self, max=0):
|
||||||
super(StoppableThread, self).__init__()
|
super(StoppableThread, self).__init__()
|
||||||
self.__stop = True
|
self.__stop = threading.Event()
|
||||||
self.__lock = threading.Lock()
|
self.__stop.set()
|
||||||
|
|
||||||
def start(self):
|
def start(self):
|
||||||
with self.__lock:
|
self.__stop.clear()
|
||||||
self.__stop = False
|
|
||||||
|
|
||||||
super(StoppableThread, self).start()
|
super(StoppableThread, self).start()
|
||||||
|
|
||||||
def join(self):
|
def join(self):
|
||||||
with self.__lock:
|
self.__stop.set()
|
||||||
self.__stop = True
|
|
||||||
|
|
||||||
super(StoppableThread, self).join()
|
super(StoppableThread, self).join()
|
||||||
|
|
||||||
def shouldRun(self):
|
def shouldRun(self):
|
||||||
with self.__lock:
|
"""Returns False is a request to stop is made.
|
||||||
return not self.__stop
|
"""
|
||||||
|
return not self.__stop.isSet()
|
||||||
|
|
||||||
|
def sleep(self, delay):
|
||||||
|
"""Sleep for some time, or until a request to stop is made.
|
||||||
|
Return value is the same as shouldRun()
|
||||||
|
"""
|
||||||
|
R = self.__stop.wait(delay)
|
||||||
|
if R is None:
|
||||||
|
R = self.__stop.isSet() # Handle python <= 2.6
|
||||||
|
return not R
|
||||||
|
|
||||||
class Worker(threading.Thread):
|
class Worker(threading.Thread):
|
||||||
"""A threaded work queue.
|
"""A threaded work queue.
|
||||||
|
Reference in New Issue
Block a user