[Twisted-Python] Graceful shutdown of twistd application

Fabio Sangiovanni sangiovanni at nweb.it
Thu Sep 4 06:36:00 MDT 2014


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)


This is what I can see in the logs:

Sep  4 14:25:06 prepyproxy01 proxy [4924]: [-] Received SIGTERM, shutting
down.
Sep  4 14:25:06 prepyproxy01 proxy [4924]: [-] from within trigger
Sep  4 14:25:06 prepyproxy01 proxy [4924]:
[TwistedProtocolConnection,client] <twisted.internet.tcp.Connector instance
at 0x0000000005717be0> will retry in 2 seconds
Sep  4 14:25:06 prepyproxy01 proxy [4924]:
[TwistedProtocolConnection,client] Stopping factory
<__builtin__.RabbitMQClientFactory instance at 0x00000000057172c0>
Sep  4 14:25:06 prepyproxy01 proxy [4924]: [-] (TCP Port 10025 Closed)
Sep  4 14:25:06 prepyproxy01 proxy [4924]: [-] Stopping factory
<__builtin__.TempfailingESMTPFactory instance at 0x00000000057172a0>
Sep  4 14:25:09 prepyproxy01 proxy [4924]: [-] Starting factory
<__builtin__.RabbitMQClientFactory instance at 0x00000000057172c0>
Sep  4 14:25:09 prepyproxy01 proxy [4924]:
[TwistedProtocolConnection,client] [rmq01] RabbitMQ connection established
Sep  4 14:25:16 prepyproxy01 proxy [4924]:
[TwistedProtocolConnection,client] <twisted.internet.tcp.Connector instance
at 0x0000000005717be0> will retry in 2 seconds
Sep  4 14:25:16 prepyproxy01 proxy [4924]:
[TwistedProtocolConnection,client] Stopping factory
<__builtin__.RabbitMQClientFactory instance at 0x00000000057172c0>
Sep  4 14:25:16 prepyproxy01 proxy [4924]: [-] Main loop terminated.
Sep  4 14:25:16 prepyproxy01 proxy [4924]: [-] Server Shut Down.

It seems to me that the shutdown phase doesn't wait for the deferred to
fire before stopping my client and server.
To be clear: my expected result is:
- SIGTERM
- pause 10s
- client/server shutdown

I am surely missing something, but I really can't figure out what.

Oh, for the records: I'm using Twisted 13.2.0 on Pypy.

Thanks!
-------------- next part --------------
An HTML attachment was scrubbed...
URL: </pipermail/twisted-python/attachments/20140904/8e167cf0/attachment-0002.html>


More information about the Twisted-Python mailing list