[Twisted-Python] service takedown (one newbie to other newbies, methinks)

Bill la Forge laforge49 at yahoo.co.in
Fri May 21 10:12:18 MDT 2004


If a service starts successfully, once initialization completes 
successfully, it's stopService will be called for proper cleanup.
 
However, if another service fails to successfully initialize, and it
is kind enough to call reactor.stop() rather than throw an exception,
then the first service can, at the end of its initialization, call
reactor.addSystemEventTrigger('before','shutdown',self.failedStartup),
then failedStartup() will be called and there is STILL a chance to
cleanup.
 
Here's the code, the script, and the twistd output:
 
from twisted.application import service
from twisted.internet import reactor
class SimpleService(service.Service):
    def startService(self):
        service.Service.startService(self)
        reactor.addSystemEventTrigger('before','shutdown',self.failedStartup)
        print '>>>start'
    def stopService(self):
        print '>>>stop'
        service.Service.stopService(self)
    def failedStartup(self):
        print 'Oh!!!!!!!!!!!!!!!!!!!!!'
        
class FailService(service.Service):
    def startService(self):
        reactor.stop()

from twisted.application import service
from twisted.internet import reactor
import simpleService
application = service.Application('testServer')
serviceCollection=service.IServiceCollection(application)
ss=simpleService.SimpleService()
ss.setServiceParent(serviceCollection)
fs=simpleService.FailService()
fs.setServiceParent(serviceCollection)

C:\other\Bill\compstrm\compstrm\twbsd>twistd -noy testService.py
2004/05/21 21:39 India Standard Time [-] Log opened.
2004/05/21 21:39 India Standard Time [-] twistd 1.2.0 (C:\Python23\python.exe 2.
3.3) starting up
2004/05/21 21:39 India Standard Time [-] reactor class: twisted.internet.default
.SelectReactor
2004/05/21 21:39 India Standard Time [-] >>>start
2004/05/21 21:39 India Standard Time [-] Oh!!!!!!!!!!!!!!!!!!!!!
2004/05/21 21:39 India Standard Time [-] Main loop terminated.
2004/05/21 21:39 India Standard Time [-] Server Shut Down.
C:\other\Bill\compstrm\compstrm\twbsd>


Bill la Forge
http://www.geocities.com/laforge49/
Yahoo! India Matrimony: Find your partner online.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: </pipermail/twisted-python/attachments/20040521/f61c4ff4/attachment.html>


More information about the Twisted-Python mailing list