【发布时间】:2012-03-17 06:55:26
【问题描述】:
我的 python 脚本创建了很多线程,它们都是守护线程,我发现我收到一个错误提示“内存不足”。
如何在我的脚本/应用程序运行时终止守护线程?
我了解守护线程的概念,即当我的进程(脚本或应用程序)关闭/完成时它们会自行销毁。但是我想在我的脚本仍在运行时杀死我的一些守护线程以避免“内存不足”错误。
当队列中没有更多任务时,我的下面的线程会自行杀死吗?
class ParsePageThread(threading.Thread):
THREAD_NUM = 0
def __init__(self, _queue):
threading.Thread.__init__(self)
self.queue = _queue
def run(self):
while(True):
try:
url = self.queue.get()
except Queue.Empty,e:
return # WILL this kill the thread?
finally:
self.queue.task_done()
【问题讨论】:
-
这听起来像是你做错了事。如果您可能会耗尽所有内存而不是杀死随机内存,那么不启动线程不是更好吗?
-
您可能会受益于更高的抽象级别,例如
ThreadPoolExecutor(重用线程来完成工作,而不是创建新线程)
标签: python multithreading