开发者

stopping a python thread using __del__

开发者 https://www.devze.com 2023-03-25 11:24 出处:网络
I have a threaded program in Python that works fine except that __del__ does not get called once the thread is running:

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.se开发者_运维技巧t()
        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


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消