# -*- Mode: Python -*- # vi:si:et:sw=4:sts=4:ts=4 # show how classes can have deferreds for their subclassable methods # and still execute in the right order # and have the child decide on what aggregate result to return from twisted.internet import reactor, defer import os import sys import signal class Parent: def stop(self): print "Parent.stop() begin" d = defer.Deferred() self.__doStopStuff(d) print "Parent.stop() end, returning %r" % d return d def __doStopStuff(self, d): print "Parent: doing stoppy stuff" reactor.callLater(1, self.__doneStopStuff, d) def __doneStopStuff(self, d): print "Parent: done stoppy stuff, callbacking %r" % d d.callback("parent") class Child(Parent): def stop(self): print "Child.stop() begin" d = defer.Deferred() self.__doStopStuff(d) #d.addCallback(lambda r: Parent.stop(self)) print "Child.stop() end, returning %r" % d return d def __doStopStuff(self, d): print "Child: doing stoppy stuff" reactor.callLater(1, self.__doneStopStuff, d) def __doneStopStuff(self, d): print "Child: done stoppy stuff" pd = Parent.stop(self) pd.addCallback(self.__processParentStop, d) def __processParentStop(self, result, d): print "Child: parent's stop returned %r" % result print "Child: callbacking %r" % d d.callback("child") def beforeShutdown(): print "before shutdown" def afterShutdown(): print "after shutdown" def doParentStuff(): p = Parent() d = p.stop() print "doParentStuff(): adding doneParentStuff callback to %r" % d d.addCallback(doneParentStuff) def doneParentStuff(result): print "doneParentStuff(): result %r" % result reactor.callLater(0, doChildStuff) def doChildStuff(): c = Child() d = c.stop() print "doChildStuff(): adding doneChildStuff callback to %r" % d d.addCallback(doneChildStuff) def doneChildStuff(result): print "doneChildStuff(): result %r" % result reactor.callLater(0, reactor.stop) reactor.addSystemEventTrigger('before', 'shutdown', beforeShutdown) reactor.addSystemEventTrigger('after', 'shutdown', afterShutdown) reactor.callLater(0, doParentStuff) reactor.run() print "done"