[Twisted-Python] How to do cleanup to server on sigint
exarkun at divmod.com
Tue Jan 31 09:23:30 EST 2006
On Tue, 31 Jan 2006 11:43:02 +0000, Rob Mason <rob.mason at ammeon.com> wrote:
>I am using twisted to write a test client to interface with a server running
>a bespoke protocol.
>The server expects interactions from the client something like:
>If the server does not receive a specific unattach from the client , it will
>maintain a resource open for the client even though there is no connection.
>Eventually the server will refuse additional connections (once it's number
>of 'open' connections exceeds some limit).
Is it intentional that resource allocation is independent of actual TCP connections? If not, perhaps you just want to clean things up when a connection is lost. You may also want to use a TCP or app-level keep alive so as to notice connections lost due to network problems.
>I want my client to be able to send an 'unattach' message before closing.
>Is there a way I can override the default reactor behaviour in handling e.g.
>SIGINT so that I can force my client to send the unattach message before
>proceeding with normal reactor sigint handling?
You don't actually care about SIGINT, just shutdown. The lowest level hook available for this is the shutdown system event, which can receive notification of via:
reactor.addSystemEventTrigger('before', 'shutdown', f)
Twisted includes a service system which takes advantage of this feature, which you may want to use instead. See <http://twistedmatrix.com/projects/core/documentation/howto/application.html>, which mostly talks about servers, but clients are approximately the same.
More information about the Twisted-Python