worker
当 while 循环离开时,您应该在从 within 调用的回调中将 thread 设置为 None :
编辑:现在还支持立即重启后台进程
import time
import threading
class CoolThing(object):
def __init__(self):
self.thread = None
def run_in_background(self, callback, period=0.5):
wait_count = 0
while True:
if self.thread:
if self.thread[1].running or wait_count>10:
raise RuntimeError()
time.sleep(0.5)
wait_count += 1
else:
break
def worker():
t0 = time.time()
worker.running = True
while worker.running:
if time.time()-t0>2:
callback()
t0 = time.time()
time.sleep(period)
worker.callback()
worker.callback = self.dispose
self.thread = (threading.Thread(target=worker), worker)
self.thread[0].start()
def stop_background(self, join=False):
if not self.thread:
raise RuntimeError
self.thread[1].running = False
if join:
self.thread[0].join()
self.stopping = True
def dispose(self):
self.thread = None
self.stopping
def my_callback():
print "Beep"
if __name__=="__main__":
cool_thing = CoolThing()
cool_thing.run_in_background(my_callback, 0.5)
time.sleep(10)
cool_thing.stop_background()
# Immediatley restart process
cool_thing.run_in_background(my_callback, 0.5)
time.sleep(10)
cool_thing.stop_background()
print cool_thing.thread
time.sleep(3)
print cool_thing.thread
给出输出:
Beep
Beep
Beep
(<Thread(Thread-2, started 10760)>, <function worker at 0x02DEDD70>)
None
所以在调用 stop_background 之后,self.thread 仍然被设置,但后来,它是None
. 您还可以保存 worker.callback-variable 并通过它的名称调用 dispose(),但是这样,代码更加灵活。
编辑 2:新要求,新代码示例
我为工人(SRP)创建了一个单独的班级,CoolThing 保存了一份此类工人的名单。如果run_background(...)
已启动,它会检查是否有任何工作人员仍在运行(未请求停止),然后引发 RuntimeError。否则,启动一个新的工人。
stop_background()
告诉每个工人停止,每个工人调用一个回调,然后从所有工人的列表中删除这个工人。
import time
import threading
class Worker(threading.Thread):
def __init__(self, callback, period=0.5, finished_callback = None):
threading.Thread.__init__(self)
self.callback = callback
self.period = period
self._stop_requested = False
self._finished_callback = finished_callback
def run(self):
t0 = time.time()
while not self._stop_requested:
if time.time()-t0>2:
self.callback()
t0 = time.time()
time.sleep(self.period)
if self._finished_callback:
self._finished_callback(self)
def request_stop(self):
self._stop_requested = True
@property
def stopping(self):
return self._stop_requested
class CoolThing(object):
def __init__(self):
self.workers = []
self.workers_lock = threading.Lock()
def run_in_background(self, callback, period=0.5):
if len([w for w in self.workers if not w.stopping])>0:
raise RuntimeError()
worker = Worker(callback, period, finished_callback=self.dispose)
with self.workers_lock:
self.workers.append(worker)
worker.start()
def stop_background(self, join=False):
if len(self.workers) == 0:
raise RuntimeError()
for worker in self.workers:
worker.request_stop()
if join:
for worker in self.workers:
worker.join()
def dispose(self, worker):
with self.workers_lock:
self.workers.remove(worker)
def my_callback():
print "Beep"
if __name__=="__main__":
cool_thing = CoolThing()
cool_thing.run_in_background(my_callback, 0.5)
time.sleep(10)
print cool_thing.workers
cool_thing.stop_background()
# Immediatley restart process
cool_thing.run_in_background(my_callback, 0.5)
print cool_thing.workers
time.sleep(5)
print cool_thing.workers
time.sleep(5)
cool_thing.stop_background()
print cool_thing.workers
time.sleep(3)
print cool_thing.workers