[Twisted-Python] Application Start Up and Tear Down
Hi, so far I've been using only one Factory in my application. So, all the intialization and deinitialization code was in startFactory() / stopFactory(). Now I have to add another protocol with another factory. Is there a similar pair of methods for the application (server) where I can initialize resources (db pool, for example) upon startup and free them upon shutdown? -- Best regards, Pavel Bastov
On 9 Jul 2007, at 00.49, Pavel Bastov wrote:
so far I've been using only one Factory in my application. So, all the intialization and deinitialization code was in startFactory() / stopFactory().
Now I have to add another protocol with another factory. Is there a similar pair of methods for the application (server) where I can initialize resources (db pool, for example) upon startup and free them upon shutdown?
Wouldn't it work to simply handle your initialization and cleanup before and after reactor.run()?
Hmm, I don't call reactor.run() explicitly. Here is how I launch it: application = service.Application('xoochat', uid = uid, gid = gid) internet.TCPServer(config.port, factory).setServiceParent(service.IServiceCollection(application)) internet.TCPServer(3711, pushFactory).setServiceParent(service.IServiceCollection(application)) On 7/9/07, Adam Atlas <adam@atlas.st> wrote:
On 9 Jul 2007, at 00.49, Pavel Bastov wrote:
so far I've been using only one Factory in my application. So, all the intialization and deinitialization code was in startFactory() / stopFactory().
Now I have to add another protocol with another factory. Is there a similar pair of methods for the application (server) where I can initialize resources (db pool, for example) upon startup and free them upon shutdown?
Wouldn't it work to simply handle your initialization and cleanup before and after reactor.run()?
_______________________________________________ Twisted-Python mailing list Twisted-Python@twistedmatrix.com http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
-- Best regards, Pavel Bastov
try reactor.addSystemEventTrigger() it calls back on system events such as startup and shutdown which are fired by the reactor. read the details in the docs: http://twistedmatrix.com/documents/current/api/twisted.internet.interfaces.I... On 7/9/07, Pavel Bastov <pbastov@gmail.com> wrote:
Hmm,
I don't call reactor.run() explicitly.
Here is how I launch it:
application = service.Application('xoochat', uid = uid, gid = gid) internet.TCPServer(config.port, factory).setServiceParent(service.IServiceCollection(application)) internet.TCPServer(3711, pushFactory).setServiceParent(service.IServiceCollection(application))
On 7/9/07, Adam Atlas <adam@atlas.st> wrote:
On 9 Jul 2007, at 00.49, Pavel Bastov wrote:
so far I've been using only one Factory in my application. So, all the intialization and deinitialization code was in startFactory() / stopFactory().
Now I have to add another protocol with another factory. Is there a similar pair of methods for the application (server) where I can initialize resources (db pool, for example) upon startup and free them upon shutdown?
Wouldn't it work to simply handle your initialization and cleanup before and after reactor.run()?
_______________________________________________ Twisted-Python mailing list Twisted-Python@twistedmatrix.com http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
-- Best regards, Pavel Bastov
_______________________________________________ Twisted-Python mailing list Twisted-Python@twistedmatrix.com http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
Thanks Gábor, this sounds like what I need. Will take a deeper look at it. On 7/9/07, Gábor Bernáth <gabor.bernath@gmail.com> wrote:
try reactor.addSystemEventTrigger()
it calls back on system events such as startup and shutdown which are fired by the reactor. read the details in the docs:
-- Good luck, Pavel Bastov xooChat Evangelist and Team Leader http://www.xoochat.com/
On Mon, 9 Jul 2007 11:18:50 +0600, Pavel Bastov <pbastov@gmail.com> wrote:
Hmm,
I don't call reactor.run() explicitly.
Here is how I launch it:
application = service.Application('xoochat', uid = uid, gid = gid) internet.TCPServer(config.port, factory).setServiceParent(service.IServiceCollection(application)) internet.TCPServer(3711, pushFactory).setServiceParent(service.IServiceCollection(application))
The right way to do this is defining your own Service like in this way: import locale from twisted.internet import defer from twisted.cred.checkers import AllowAnonymousAccess from twisted.python.components import registerAdapter from twisted.cred.credentials import IAnonymous from twisted.application.service import Service from twisted.cred.portal import Portal from nevow import inevow, appserver #from nevow import guard from starter.monkeypatches import VhostFakeRoot from starter import storage, auth, guard, istarter from starter.web import main class StarterService(Service): def __init__(self, conf): self.conf = conf def privilegedStartService(self): locale.setlocale(locale.LC_TIME, self.conf['general']['lc_ctime']) dbconf = self.conf['db'] self.db = p = storage.init(dbconf['engine']) p.init(dbconf['strategy'], dbconf['database'], dbconf['host'], dbconf['port'], dbconf['user'], dbconf['password']) ls = auth.LoginSystem(self.db, self.conf) p = Portal(ls) p.registerChecker(AllowAnonymousAccess(), IAnonymous) p.registerChecker(ls) registerAdapter(main.Root, istarter.IAvatar, inevow.IResource) res = guard.SessionWrapper(p) vhost = VhostFakeRoot(res) self.site = appserver.NevowSite(vhost, logPath=self.conf['paths']['web'].path) from twisted.internet import reactor self.port = reactor.listenTCP(self.conf['web']['port'], self.site, interface=self.conf['web']['interface']) def stopService(self): dl = [] if self.port is not None: dl.append(defer.maybeDeferred(self.port.stopListening)) self.port = None if self.db is not None: dl.append(defer.maybeDeferred(self.db.close)) self.db = None return defer.DeferredList(dl) And you define your .tac file like this: from twisted.application import service from starter.service import StarterService from configuration import main application = service.Application('Starter') StarterService(main).setServiceParent(application) Of course Twisted will call Service.privilegedStartService when everything startsup and after it changed UID (or anyway after Service.startService). Then when everything stops it will call Service.stopService. You can of course register other services as children of this parent one by using .setServiceParent(). -- Valentino Volonghi aka dialtone Blog: http://www.twisted.it
On Mon, 9 Jul 2007 09:27:21 +0200, Valentino Volonghi aka Dialtone <dialtone@divmod.com> wrote:
Of course Twisted will call Service.privilegedStartService when everything startsup and after it changed UID (or anyway after Service.startService).
Of course this is wrong. Service.privilegedStartService() switchUID and fork shedPriviledges() Service.startService()
Then when everything stops it will call Service.stopService.
-- Valentino Volonghi aka dialtone Blog: http://www.twisted.it
Pavel Bastov wrote:
Hi,
so far I've been using only one Factory in my application. So, all the intialization and deinitialization code was in startFactory() / stopFactory().
Now I have to add another protocol with another factory. Is there a similar pair of methods for the application (server) where I can initialize resources (db pool, for example) upon startup and free them upon shutdown?
Make your own Service subclass, and override startService/stopService. -Andrew.
On 05:53 am, andrew-twisted@puzzling.org wrote:
Pavel Bastov wrote:
Hi,
so far I've been using only one Factory in my application. So, all the intialization and deinitialization code was in startFactory() / stopFactory().
Now I have to add another protocol with another factory. Is there a similar pair of methods for the application (server) where I can initialize resources (db pool, for example) upon startup and free them upon shutdown?
Make your own Service subclass, and override startService/stopService.
I just want to reinforce that this is really the *correct* answer, and the other suggestions (custom code before/after run(), manually calling addSystemEventTrigger) both tightly bind your application logic to the particular way your entire process starts up and shuts down, rather than having a discrete unit for your application. (Think global vs. instance variables.) The service hierarchy has issues (privilegedStartService being the biggest one) but it is generally the most flexible, since you can start and stop services and groups of services without starting and stopping the entire process.
Thanks all for your assistance, the service subclassing way worked perfectly. Now I can handle several protocols with the service and share common data among them. On 7/9/07, glyph@divmod.com <glyph@divmod.com> wrote:
On 05:53 am, andrew-twisted@puzzling.org wrote:
Pavel Bastov wrote:
Hi,
so far I've been using only one Factory in my application. So, all the intialization and deinitialization code was in startFactory() / stopFactory().
Now I have to add another protocol with another factory. Is there a similar pair of methods for the application (server) where I can initialize resources (db pool, for example) upon startup and free them upon shutdown?
Make your own Service subclass, and override startService/stopService.
I just want to reinforce that this is really the *correct* answer, and the other suggestions (custom code before/after run(), manually calling addSystemEventTrigger) both tightly bind your application logic to the particular way your entire process starts up and shuts down, rather than having a discrete unit for your application. (Think global vs. instance variables.)
The service hierarchy has issues (privilegedStartService being the biggest one) but it is generally the most flexible, since you can start and stop services and groups of services without starting and stopping the entire process.
_______________________________________________ Twisted-Python mailing list Twisted-Python@twistedmatrix.com http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
-- Good luck, Pavel Bastov xooChat Evangelist and Team Leader http://www.xoochat.com/
participants (6)
-
Adam Atlas
-
Andrew Bennetts
-
glyph@divmod.com
-
Gábor Bernáth
-
Pavel Bastov
-
Valentino Volonghi aka Dialtone