[Twisted-Python] Graceful shutdown of twistd application

exarkun at twistedmatrix.com exarkun at twistedmatrix.com
Thu Sep 4 06:55:49 MDT 2014


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




More information about the Twisted-Python mailing list