[Twisted-Python] Graceful shutdown of twistd application
Fabio Sangiovanni
sangiovanni at nweb.it
Fri Sep 5 02:42:17 MDT 2014
Hi,
I just wanted to give an update about the way I got it, in case this could
be useful for somebody.
I essentially inherited from service.MultiService like this:
class GracefulMultiService(service.MultiService):
def stopService(self):
d = defer.succeed(None)
d.addCallback(self._wait_for_your_cleanup_code_to_complete)
d.addCallback(lambda _: self)
d.addCallback(service.MultiService.stopService)
return d
@defer.inlineCallbacks
def _wait_for_your_cleanup_code_to_complete(self):
[...]
yield sleep(10) # cleanup code example
def sleep(secs):
d = defer.Deferred()
reactor.callLater(secs, d.callback, None)
return d
Thanks again for your help!
On Thu, Sep 4, 2014 at 2:55 PM, <exarkun at twistedmatrix.com> wrote:
> On 12:36 pm, sangiovanni at nweb.it wrote:
>
>> On Thu, Sep 4, 2014 at 2:02 PM, <exarkun at twistedmatrix.com> wrote:
>>
>>>
>>>
>>> You said before shutdown triggers are too late but you didn't say why. I
>>> think that's based on a misunderstanding - but if not, then explain why
>>> it
>>> doesn't work for your scenario.
>>>
>>
>> Hi, thanks for your reply.
>>
>> I've tried the following:
>>
>> def sleep(secs):
>> log.msg('from within trigger')
>> d = defer.Deferred()
>> reactor.callLater(secs, d.callback, None)
>> return d
>>
>> reactor.addSystemEventTrigger('before', 'shutdown', sleep, 10)
>>
>
> All 'before' trigger are run concurrently. If you're using `Application`
> then your `sleep` trigger runs concurrently with the application's
> `stopService` trigger (because `Application` has its stopService added as
> another 'before' shutdown' trigger alongside yours).
>
> If you want to delay your application shutdown, you need to cooperate a
> little more closely with it. Either attach your application shutdown code
> as a callback to the sleep Deferred or move the sleep into the stopService
> implementation of one of the services on your application and trigger the
> remaining stopService calls (eg the stopService call on the MultiService
> you mentioned) when the sleep Deferred fires there.
>
> Jean-Paul
>
>
> _______________________________________________
> Twisted-Python mailing list
> Twisted-Python at twistedmatrix.com
> http://twistedmatrix.com/cgi-bin/mailman/listinfo/twisted-python
>
>
--
*Fabio Sangiovanni | System Integrator*
*T* +39 0372 24525* F* +39 0372 800725
Via Dei Comizi Agrari 10, 26100 Cremona - Italia
Milano - Cremona - San Francisco
MailUpĀ® La soluzione per l'invio di email e SMS mailup.it
<http://www.mailup.it>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: </pipermail/twisted-python/attachments/20140905/27395464/attachment-0002.html>
More information about the Twisted-Python
mailing list