[Twisted-Python] twistd and win32 service

Itamar Shtull-Trauring itamar at itamarst.org
Wed Oct 22 14:50:10 EDT 2003


On Wed, 22 Oct 2003 12:24:42 -0600
"Justin Johnson" <justinjohnson at fastmail.fm> wrote:

> The general consensus on the mailing list archives seems to be that
> I'd need to setup my code to not require twistd but just run
> standalone. 

Uh. *Maybe* that's the consensus, but it's not actually correct. You can
have a tap or tac run as a NT service.

Lets say you have  a script "server.py" that is runnable with "twistd
-y", you can do (and similar code will work for TAPs):

import sys, os
import win32serviceutil, win32service


class MyService(win32serviceutil.ServiceFramework):
    """NT Service."""
    
    _svc_name_ = "MyService"
    _svc_display_name_ = "MyService server"

    def SvcDoRun(self):
        import server
        f = open(os.path.join(server.rootPath, "cyberhigh.log"), 'a')
        from twisted.python.log import startLogging
        from twisted.application.app import startApplication
        from twisted.internet import reactor        
        startLogging(f)
        startApplication(server.application, 0)
        reactor.run()
    
    def SvcStop(self):
        self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
        from twisted.internet import reactor
        reactor.stop()


if __name__ == '__main__':
    win32serviceutil.HandleCommandLine(MyService)


-- 
Itamar Shtull-Trauring    http://itamarst.org/
Available for Python & Twisted consulting




More information about the Twisted-Python mailing list