假设我有这段代码(尚未编写实际的计时器):
class Timer(threading.Thread):
def __init__(self, seconds):
self.runTime = seconds
threading.Thread.__init__(self)
def run(self):
time.sleep(self.runTime)
#do some other function
print 'Finished'
t = Timer(60)
t.start()
一旦 run() 方法完成运行,是否有某种方法可以停止线程并删除类实例?
最佳答案
run()
方法是在线程中运行的所有内容,因此当它完成后,就不再运行任何内容。那么t.is_alive()
将返回False
。然后,您可以使用 del t
删除该实例,但这只会删除对它的引用。实际删除将由垃圾收集器稍后完成。
关于python - 删除线程定时器类?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10218033/