[Twisted-Python] Graceful shutdown of twistd application

exarkun at twistedmatrix.com exarkun at twistedmatrix.com
Thu Sep 4 06:02:48 MDT 2014


On 10:52 am, sangiovanni at nweb.it wrote:
>Hello list,
>
>I need to implement a graceful shutdown procedure for a twistd 
>application.
>The application is made up of two services: an internet.TCPClient and
>an internet.TCPServer.
>They're glued together with a MultiService instance, which is in turn 
>set
>to have 'application' as parent.
>The server and the client work together, making a proxy (SMTP server 
>and
>AMQP client).
>
>My goal is the following:
>- intercept a SIGTERM signal
>- 'block' on the server side: since it's SMTP I get this by setting a
>variable that makes the server return tempfails (4xx) for new messages,
>while keeping current sessions active
>- wait until current requests are satisfied (I keep a dictionary of 
>current
>pending messages)
>- shut the whole thing down

This is exactly what before shutdown triggers are for.  Alternatively, 
use the higher-level API and implement `stopService` on one of your 
services.

Either way, return a `Deferred` that only fires when you're satisfied it 
is time for shutdown to proceed.

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.

Jean-Paul
>What is the best solution for this use case? It's not really clear to 
>me
>how to catch SIGTERM and handle pending requests *before* the 
>underlying
>services start to shutdown (i.e. even addSystemEventTrigger('before',
>'shutdown', callable) is called too late for my needs).
>
>Thank you very much for your help!
>
>Fabio




More information about the Twisted-Python mailing list