【发布时间】:2011-10-06 21:20:30
【问题描述】:
我有一个函数,它接受一个大数组 x,y 对作为输入,它使用 numpy 和 scipy 进行一些复杂的曲线拟合,然后返回一个值。为了尝试加快速度,我尝试使用两个线程将数据提供给使用 Queue.Queue 。一旦数据完成。我试图让线程终止,然后结束调用进程并将控制权返回给 shell。
我试图理解为什么我必须求助于 threading.Thread 中的私有方法来停止我的线程并将控制权返回给命令行。
self.join() 不会结束程序。重新获得控制权的唯一方法是使用私有停止方法。
def stop(self):
print "STOP CALLED"
self.finished.set()
print "SET DONE"
# self.join(timeout=None) does not work
self._Thread__stop()
这是我的代码的近似值:
class CalcThread(threading.Thread):
def __init__(self,in_queue,out_queue,function):
threading.Thread.__init__(self)
self.in_queue = in_queue
self.out_queue = out_queue
self.function = function
self.finished = threading.Event()
def stop(self):
print "STOP CALLED"
self.finished.set()
print "SET DONE"
self._Thread__stop()
def run(self):
while not self.finished.isSet():
params_for_function = self.in_queue.get()
try:
tm = self.function(paramsforfunction)
self.in_queue.task_done()
self.out_queue.put(tm)
except ValueError as v:
#modify params and reinsert into queue
window = params_for_function["window"]
params_for_function["window"] = window + 1
self.in_queue.put(params_for_function)
def big_calculation(well_id,window,data_arrays):
# do some analysis to calculate tm
return tm
if __name__ == "__main__":
NUM_THREADS = 2
workers = []
in_queue = Queue()
out_queue = Queue()
for i in range(NUM_THREADS):
w = CalcThread(in_queue,out_queue,big_calculation)
w.start()
workers.append(w)
if options.analyze_all:
for i in well_ids:
in_queue.put(dict(well_id=i,window=10,data_arrays=my_data_dict))
in_queue.join()
print "ALL THREADS SEEM TO BE DONE"
# gather data and report it from out_queue
for i in well_ids:
p = out_queue.get()
print p
out_queue.task_done()
# I had to do this to get the out_queue to proceed
if out_queue.qsize() == 0:
out_queue.join()
break
# Calling this stop method does not seem to return control to the command line unless I use threading.Thread private method
for aworker in workers:
aworker.stop()
【问题讨论】:
-
sys.exit()(仅杀死线程) -
self.daemon = True仅在调用 start() 之前设置有效,否则引发 RuntimeError -
sys.exit()不会杀死线程,但会在当前线程中引发 SystemExit 异常。
标签: python multithreading queue