Stop python thread using __del__

I have a program with threads in Python that works fine, except that __del__ is not called after the thread starts:

class tt(threading.Thread):
    def __init__(self):
        threading.Thread.__init__(self)
        self.stop_event = threading.Event()

    def __del__(self):
        print "Deleting"
        self.stop_event.set()
        time.sleep(5)

    def run(self):
        self.stop_event.clear()
        while not self.stop_event.isSet():
             self.do_stuff()
        threading.Thread.__init__(self)

    def stop(self):
        self.stop_event.set()

For example, if I do

tc = aa()
del tc 

It works fine (I get the removal of the pause message, etc.). However, if I do this:

tc = aa()
tc.start()
del tc

Then __del__ does not start (note that it does __del__ when I execute tc.start (); tc.stop (); del tc.

I run this in ipython

+5
source share
2 answers

__del__() , , del , . ( , gc'ed).

A threading.Thread , , . , , , , , .

+6

:

del x x.__del__() - x , , x .

, () . , . , __del__ .

+3

All Articles