简体   繁体   中英

Deleting a threaded timer class?

Imagine I have this code (not written the actual timer yet):

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()

Once the run() method has finished running, is there some way to stop the thread, and delete the class instance?

The run() method is everything that runs in a thread, so when it has finished nothing is running anymore. Then t.is_alive() will return False . You can then delete the instance using del t , but that will just remove your reference to it. The actual deleting will be done by garbage collector some time later.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM