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

Andrew Bennetts andrew-twisted at puzzling.org
Sun Dec 9 21:46:17 EST 2001


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.





More information about the Twisted-Python mailing list