[Twisted-Python] Could Service.startService return a Deferred?
Peter Westlake
peter.westlake at pobox.com
Wed Nov 27 11:00:46 MST 2013
On Wed, Nov 27, 2013, at 17:13, exarkun at twistedmatrix.com wrote:
> On 02:58 pm, peter.westlake at pobox.com wrote:
> ... So would it be possible for
> >t.a.s.Service.startService
> >to be allowed to return a Deferred? Then the next service would only be
> >started up when the Deferred fired.
>
> Probably not.
>
> There is some discussion on
> <https://twistedmatrix.com/trac/ticket/5941>.
That's helpful, thanks. I hadn't realized that startService was called
before the reactor started.
Instead, I've passed in a Deferred to each service that needs to wait.
How does this look?
# -*- mode: python -*-
from twisted.application import service
from twisted.internet import defer, reactor
from twisted.internet.task import deferLater
application = service.Application('Dependencies')
def report(error):
print 'ERROR', error
reactor.stop()
class Runner(service.Service):
def __init__(self, baton):
self.baton = baton
def startService(self):
print 'startService', self.name, reactor.running
self.baton.addCallback(lambda ignore:deferLater(reactor, 1.0,
self.realStartService))
self.baton.addErrback(report)
def realStartService(self):
print 'realStartService', self.name, reactor.running
baton = defer.succeed('pass me to each service')
foo = Runner(baton)
foo.setName('foo')
foo.setServiceParent(application)
bar = Runner(baton)
bar.setName('bar')
bar.setServiceParent(application)
Peter.
More information about the Twisted-Python
mailing list