[Twisted-Python] Running a twisted server as a WinNT service

This is just a quick mail to let people know what I had to do to make a Twisted server work as a WinNT service. First, it should be noted that I'm not using twistd or tap files, partly because this server has no state that needs persisting, and also because I'm not sure how to use them ;) Basically, the service is really really simple: --- import win32serviceutil, win32service import MyServer from twisted.python.log import startLogging import twisted.internet.main class MyTwistedService(win32serviceutil.ServiceFramework): def SvcDoRun(): # Can't use stdout for logging -- .flush will barf startLogging(open('c:/mylogfile.log','a')) MyServer.main() def SvcStop(): self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING) twisted.internet.main.shutDown() if __name__ == '__main__': win32serviceutil.HandleCommandLine(MyTwistedService) --- And that does it. The trickiest bit is that you need to set the log file to something other than stdout, otherwise it dies due to a Bad File Descriptor error doing logfile.flush(). Other than that, it is basically boilerplate code (if you're familiar with Win32 services written in Python). I wonder if there is a more integrated way to do this, though? -Andrew.

On Sun, 2001-12-09 at 20:46, Andrew Bennetts wrote:
This is just a quick mail to let people know what I had to do to make a Twisted server work as a WinNT service.
Thanks a lot, Andrew!
First, it should be noted that I'm not using twistd or tap files, partly because this server has no state that needs persisting, and also because I'm not sure how to use them ;)
[snip service]
And that does it. The trickiest bit is that you need to set the log file to something other than stdout, otherwise it dies due to a Bad File Descriptor error doing logfile.flush(). Other than that, it is basically boilerplate code (if you're familiar with Win32 services written in Python).
I wonder if there is a more integrated way to do this, though?
Well, the "integrated" way to do it would probably to be something similar to moshe's 'tap2deb' -- a Twisted service would probably be a .tap or .tac file which got loaded, similar to the way twistd does, and used the 'startLogging' call to open a log file and start writing to it. A good default would be the doc/examples/coil.tac currently in CVS (and in the coopweb.org "breakfast builds"). Does win32 have a standardized logging mechanism? Twisted can log to any file-like object, so if we could put log messages wherever winnt expects to find them that would be good ;-) (I vaguely recall something called the "Event Log"?) -- ______ you are in a maze of twisted little applications, all | |_\ remarkably consistent. | | -- glyph lefkowitz, glyph @ twisted matrix . com |_____| http://www.twistedmatrix.com/

On Tue, Dec 11, 2001 at 10:49:20PM -0600, Glyph Lefkowitz wrote:
On Sun, 2001-12-09 at 20:46, Andrew Bennetts wrote:
I wonder if there is a more integrated way to do this, though?
Well, the "integrated" way to do it would probably to be something similar to moshe's 'tap2deb' -- a Twisted service would probably be a .tap or .tac file which got loaded, similar to the way twistd does, and used the 'startLogging' call to open a log file and start writing to it. A good default would be the doc/examples/coil.tac currently in CVS (and in the coopweb.org "breakfast builds").
Hmm. I'm not sure how this would work... Python supports WinNT services by providing a special executable "PythonService.exe", which runs the script on your behalf, and passes things like "Stop" messages to your SvcStop method. The HandleCommandLine bit at the end of that script I gave is a conveniece thing to wrap up installation and removal of services -- typical usage of a script is: myservice.py install # installs the service myservice.py start # starts the service, this can also be # done via the control panel myservice.py remove # removes the service from the system Services are just processes that can be automatically started on boot, and can be stopped and started. My point here is that whatever twisted does, it will need to do it inside of a class with SvcDoRun, etc, that needs to be registered as a service. Apologies if I've just told you what you already know...
Does win32 have a standardized logging mechanism? Twisted can log to any file-like object, so if we could put log messages wherever winnt expects to find them that would be good ;-) (I vaguely recall something called the "Event Log"?)
It does, but it's more for logging "events" than individual messages, i.e. each line of the current log file should *not* be an entry in the NT event log, unless you're okay with spamming the log ;) Typically, programs will only report unusual events in the Event Log, e.g. "could not replicate filesystem", "the service 'blah' could not be started", etc. There is a notion of severity, though. Basically, I'm not sure that the NT Event Log is going to properly fit the way Twisted logs, especially seeing as services failing already gets logged automatically, and PythonService.exe sees to it that the traceback is included. I'm not sure of any better standard places though. I believe IIS typically logs to %SYSTEMROOT%\Logs\W3SVC\yyyymmdd.log (SYSTEMROOT is an environment variable that typically is set to C:\WINNT). Perhaps a similar scheme would be appropriate for Twisted? If you are interested in the Event Log route, just Google for "python event log", or read the relevant chapter of the _Programming Python on Win32_ book. -Andrew.

On Wed, 12 Dec 2001 18:54:30 +1100, Andrew Bennetts wrote:
I'm not sure of any better standard places though. I believe IIS typically logs to %SYSTEMROOT%\Logs\W3SVC\yyyymmdd.log (SYSTEMROOT is an environment variable that typically is set to C:\WINNT). Perhaps a similar scheme would be appropriate for Twisted?
If it comes to debug logging, the best way is to use OutputDebugString(). Ciao, Jürgen
participants (4)
-
Andrew Bennetts
-
Glyph Lefkowitz
-
Itamar Shtull-Trauring
-
j.her@t-online.de