I am looking for a way to inspect the state of my Twisted-based program, so I can determine the number of connected clients and have a look at other metrics I collect (such as the time of connection establishment or the time of last contact).
My thought was to add a signal handler for SIGUSR1
to the process, such that when the process receives it, it will dump the state to a file in a known location. There are several problems with this:
- Twisted overrides the signal handler with its own handler, because when the signal arrives the process ends with "user signal 1 received" on
stdout
and my code is not invoked - archaeological research on a mailing list revealed that Twisted will not override a handler for
SIGINT
; however that is no the case (Python 2.7.11 with Twisted 16.1.1), the process quits with "KeyboardInterrupt" onstdout
. - other resources suggest
reactor.run(installSignalHandlers=False)
orreactor.run(installSignalHandlers=0)
, but this appears to have no effect.
Thus I have several questions:
- what is the ideologically correct way to handle signals? (if it is possible at all)
- what is the recommended way to implement such "state introspection" facilities for Twisted servers? (I was thinking of making it listen for TCP connections on another port, and use that as an alternative for a POSIX signal - but I feel that I am making things too complicated).
Thank you for taking your time to read this, I am looking forward to hints from the hive mind of this planet.
This is the relevant excerpt from the mailing list:
> If you do this, you'll break spawnProcess. Fortunately, if you just > install a SIGINT handler, Twisted won't stomp on it: > exarkun at charm:~$ python > Python 2.5.2 (r252:60911, Jul 31 2008, 17:28:52) [GCC 4.2.3 > (Ubuntu 4.2.3-2ubuntu7)] on linux2 > Type "help", "copyright", "credits" or "license" for more information. > >>> def f(*a): > ... print 'sigint' > ... >>> import signal > >>> signal.signal(signal.SIGINT, f) > > >>> from twisted.internet import reactor > >>> reactor.run() > sigint > sigint > sigint > Quit