[Twisted-web] Custom __del__ not run

Jp Calderone exarkun at divmod.com
Tue Jul 26 06:39:07 MDT 2005

On Tue, 26 Jul 2005 19:49:27 +1000, Rasjid Wilcox <rasjidw at openminddev.net> wrote:
>Hello again.
>I have my xmlrpc class that I am running in twisted.  I want it to do some
>cleaning up when it is finished, and so I have defined a custom __del__

"Finished"?  "__del__" isn't a "finished" callback.  It's a garbage collection  callback.

>However, when mixed with a looping call, it never gets called.  Sample code
>below.  Remove the two lines starting with self.looping, and it all works as
>expected.  Put them in, and the custom __del__ is not called.

You created a strong, permanent reference to your instance.  Now it will never be garbage collected.  So there's no reason for __del__ to ever be called.

>Is this a bug in twisted?  Or have I broken some twisted rule about using
>__del__ procedures?  Is there some other way of saying 'when you have stopped
>the reactor, do foo', that twisted would be happy with?

If you want to run something when the reactor stops, you either want to use a server or a system event trigger:

    from twisted.application import service

    class SomeService(service.Service):
        def stopService(self):
            print 'bye bye'

    # Having a reference to theApplicationOrAnotherService is left 
    # as an exercise for the reader


    from twisted.internet import reactor

    class MyThing:
        def __init__(self):
            reactor.addSystemEventTrigger('before', 'shutdown', self.stop)

        def stop(self):
            print 'bye bye'

    th = MyThing()

In general, don't use __del__.  It doesn't do what you want.


More information about the Twisted-web mailing list