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__ procedure. 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. 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? Any suggestions welcome. :-) Thanks, Rasjid --------------------------------- from twisted.web import xmlrpc import xmlrpclib from twisted.internet import reactor, task class Hello(xmlrpc.XMLRPC): def __init__(self): print 'Init class Hello' # the following two lines cause the custom __del__ # function to never be called self.looping = task.LoopingCall(self.foo) self.looping.start(5) def __del__(self): print 'Tidy up class Hello' def foo(self): print 'foo!' def xmlrpc_hello(self): """Return 'hello, world'.""" return 'hello, world!' def xmlrpc_quit(self): reactor.callLater(0.1, reactor.stop) return 'Shutting down server' def main(): from twisted.web import server r = Hello() reactor.listenTCP(7080, server.Site(r), interface = '127.0.0.1') reactor.run() if __name__ == '__main__': main() -- Rasjid Wilcox Melbourne, Australia (UTC +10 hrs)
On Tue, 26 Jul 2005 19:49:27 +1000, Rasjid Wilcox <rasjidw@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__ procedure.
"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 SomeService().setServiceParent(theApplicationOrAnotherService) Alternatively 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. Jp
On Tue, 26 Jul 2005 10:39 pm, Jp Calderone wrote:
"Finished"? "__del__" isn't a "finished" callback. It's a garbage collection callback.
Oh. I had the misconception that the __del__ routine would always be called eventually, even if it was not until the interpreter was about to exit. However, this seems not to be the case.
Alternatively
from twisted.internet import reactor
class MyThing: def __init__(self): reactor.addSystemEventTrigger('before', 'shutdown', self.stop)
def stop(self): print 'bye bye'
th = MyThing()
This seems to work perfectly. Thanks. Rasjid. -- Rasjid Wilcox Melbourne, Australia (UTC +10 hrs)
participants (2)
-
Jp Calderone
-
Rasjid Wilcox