2012-04-18 42 views
1

想象我有这样的代码(不写入实际的定时器还):删除线程计时器类?

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()方法运行完毕后,是有一些方法来停止线程,并删除类的实例?

回答

3

run()方法是在线程中运行的所有东西,所以当它完成时什么都不在运行了。然后t.is_alive()将返回False。然后,您可以使用del t删除实例,但这只会删除您对该实例的引用。实际的删除将在一段时间后由垃圾收集器完成。

+0

显然,评论必须超过15个字符,所以我不能只是说谢谢,你一直是一个真正的帮助^^ – Billie 2012-04-18 21:22:59