<span class="Apple-style-span" style="font-family: arial, sans-serif; font-size: 13px; background-color: rgb(255, 255, 255); "><div>I have been looking for some info on running twisted as a Windows Service.</div><div><br></div>
<div>I have found various examples in mailing-lists and blogs that vary in what seem to be important respects.</div><div><br></div><div>I have included them below as 3 examples.</div><div><br></div><div>The problem I have is working out which scheme or combination I should be adopting.</div>
<div>I have tried out some of the options and they appear to work. But I need something better than seems to work.</div><div><br></div><div>My gut reaction is that I should be putting all my imports into SvcDoRun, since they will be used in the thread.</div>
<div><br></div><div>But if I import the reactor in SvcDoRun, should I be using reactor.callfromthread(reactor.stop). I think not</div><div><br></div><div>I think the use of waitforobject is the right thing to do as well without fully understanding it at the moment.</div>
<div><br></div><div>If anyone can throw some light on what to do I shall be very grateful.</div><div><br></div><div>It could be that I should post this question to python-windows mailing list since it seems to me more pertinent to windows than twisted.</div>
<div><br></div><div>Thanks for any info.</div><div><br></div><div>John Aherne</div><div><br></div><div>Here is 1st example.</div><div><br></div><div>The reactor is imported globally not in SvcDoRun</div><div>It uses the waitforobject to detect stopping the service</div>
<div>The reactor.stop is calledfromthread</div><div><br></div><div><br></div><div>[Twisted-Python] How to run Twisted as a service in Windows?</div><div><br></div><div>Thomas Jacob jacob at <a href="http://internet24.de/" target="_blank" style="color: rgb(0, 0, 204); ">internet24.de</a> </div>
<div>Wed Aug 9 10:49:30 EDT 2006</div><div>Previous message: [Twisted-Python] How to run Twisted as a service in Windows?</div><div>Next message: [Twisted-Python] How to run Twisted as a service in Windows?</div><div>Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]</div>
<div>AFAIK, twistd doesn&#39;t provide direct support for Windows Services yet</div><div>(Is this planned?).</div><div><br></div><div>But you can easily wrap a reactor,run() yourself by doing something</div><div>like the following using the Win32-Python packages</div>
<div><br></div><div>import win32serviceutil</div><div>import win32service</div><div>import win32event</div><div><br></div><div>from twisted.internet import reactor</div><div><br></div><div>import sys</div><div><br></div><div>
<br></div><div>class IMSAgentBase(win32serviceutil.ServiceFramework):</div><div>    _svc_name_ = &quot;myService&quot;</div><div>    _svc_display_name_ = &quot;My little Service&quot;</div><div>    _svc_description_ = &quot;My little Service&quot; # Win2k or later</div>
<div>    _svc_deps_ = [&quot;RpcSs&quot;] # Start after the Network has come up...</div><div><br></div><div>    def __init__(self, args):</div><div>        win32serviceutil.ServiceFramework.__init__(self, args)</div><div>
        self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)</div><div><br></div><div>    def SvcStop(self):</div><div>        self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)</div><div>        reactor.callFromThread(reactor.stop)</div>
<div>        win32event.SetEvent(self.hWaitStop)</div><div><br></div><div>    def SvcDoRun(self):</div><div>        </div><div>        # initialize your services here</div><div>        reactor.run()</div><div>        win32event.WaitForSingleObject(self.hWaitStop,win32event.INFINITE)</div>
<div><br></div><div>def HandleCommandLine(cls):</div><div>    win32serviceutil.HandleCommandLine(cls)</div><div><br></div><div><br></div><div>Run the above as a script.</div><div><br></div><div><br></div><div>Here is the 2nd example. </div>
<div><br></div><div>The imports are global not in SvcDoRun</div><div><br></div><div>And the reactor .stop is not called from thread.</div><div><br></div><div>And the wait for stop event is in a timeout loop</div><div><br>
</div><div>It uses waitforobject events</div><div><br></div><div>It sets installsignalhandlers to 0</div><div><br></div><div> You can then test it out with the sample Echo client from the core docs.</div><div> </div><div>
 &quot;&quot;&quot;qotdservice.py</div><div> Sample Twisted Windows Service</div><div> &quot;&quot;&quot;</div><div> </div><div> # Service Utilities</div><div> import win32serviceutil</div><div> import win32service</div><div>
 import win32event</div><div> </div><div> # Twisted imports</div><div> from twisted.internet.protocol import Protocol, Factory</div><div> from twisted.internet import reactor</div><div> </div><div> class QOTD(Protocol):</div>
<div> </div><div>     def connectionMade(self):</div><div>         self.transport.write(&quot;An apple a day keeps the doctor away\r\n&quot;)</div><div>         self.transport.loseConnection()</div><div> </div><div> </div>
<div> class WindowsService(win32serviceutil.ServiceFramework):</div><div>     _svc_name_ = &quot;TwistedWin32Service&quot;</div><div>     _svc_display_name_ = &quot;Twisted Win32 Service&quot;</div><div> </div><div>     def __init__(self, args):</div>
<div>         win32serviceutil.ServiceFramework.__init__(self, args)</div><div>         self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)</div><div> </div><div>     def SvcStop(self):</div><div>         self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)</div>
<div>         win32event.SetEvent(self.hWaitStop)</div><div> </div><div>     def SvcDoRun(self):</div><div>         import servicemanager</div><div> </div><div>         self.CheckForQuit()</div><div> </div><div>         factory = Factory()</div>
<div>         factory.protocol = QOTD</div><div> </div><div>         reactor.listenTCP(8007, factory)</div><div>         reactor.run(installSignalHandlers=0)</div><div> </div><div> </div><div>     def CheckForQuit(self):</div>
<div>         retval = win32event.WaitForSingleObject(self.hWaitStop, 10)</div><div>         if not retval == win32event.WAIT_TIMEOUT:</div><div>             # Received Quit from Win32</div><div>             reactor.stop()</div>
<div> </div><div>         reactor.callLater(1.0, self.CheckForQuit)</div><div> </div><div> if __name__==&#39;__main__&#39;:</div><div>     win32serviceutil.HandleCommandLine(WindowsService)</div><div> </div><div><br></div>
<div>Here is  the 3rd example.</div><div><br></div><div>The imports are done in SvcDoRun.</div><div>There is no callfrom thread</div><div>It does not use the waitforobject</div><div><br></div><div>import sys, os</div><div>
import win32serviceutil, win32service</div><div><br></div><div>class MyService(win32serviceutil.ServiceFramework):</div><div>    &quot;&quot;&quot;NT Service.&quot;&quot;&quot;</div><div><br></div><div>    _svc_name_ = &quot;MyService&quot;</div>
<div>    _svc_display_name_ = &quot;MyService server&quot;</div><div><br></div><div>    def SvcDoRun(self):</div><div>        import server</div><div>        f = open(os.path.join(server.rootPath, &quot;cyberhigh.log&quot;), &#39;a&#39;)</div>
<div>        from twisted.python.log import startLogging</div><div>        from twisted.application.app import startApplication</div><div>        from twisted.internet import reactor</div><div>        startLogging(f)</div>
<div>        startApplication(server.application, 0)</div><div>        reactor.run()</div><div><br></div><div>    def SvcStop(self):</div><div>        self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)</div><div>
        from twisted.internet import reactor</div><div>        reactor.stop()</div><div><br></div><div>if __name__ == &#39;__main__&#39;:&lt;/pre&gt;</div><div>    win32serviceutil.HandleCommandLine(MyService)&lt;/pre&gt;</div>
<div><br></div></span>