Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

stopping a python thread using __del__

I have a threaded program in Python that works fine except that __del__ does not get called once the thread is running:

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 deleting message pause etc). However if I do:

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

Then the __del__ does not get run (Note it does execute __del__ when I do tc.start(); tc.stop(); del tc.

I'm running this in ipython

like image 252
Ross W Avatar asked Aug 03 '11 15:08

Ross W


2 Answers

The __del__() method is executed when an instance is garbage collected, not when you call del on a name that happens to point to the instance. The latter will only delete the name (which might in some cases result in the instance pointed to by the name being gc'ed).

A threading.Thread instance will never be garbage collected while the thread is still running, since the instance is still in use in this case. And ditching a name that happens to point to the instance certainly won't stop the thread.

like image 112
Sven Marnach Avatar answered Sep 28 '22 00:09

Sven Marnach


See the documentation:

del x doesn’t directly call x.__del__() — the former decrements the reference count for x by one, and the latter is only called when x‘s reference count reaches zero.

In your case, the thread is still running and there is still some (internal) reference to it. Therefore it has a reference count greater than one, and still greater than zero when you delete it from your module namespace. Since it is not garbage-collected, __del__ is not called.

like image 43
Ferdinand Beyer Avatar answered Sep 28 '22 02:09

Ferdinand Beyer