[Twisted-Python] Running a PBServerFactory TCP Server as a Win32 Service.

Itamar Shtull-Trauring itamar at itamarst.org
Mon Jan 23 14:16:40 EST 2006


On Mon, 2006-01-23 at 18:27 +0000, william.lewis at uk.bnpparibas.com
wrote:
> 
> Hi 
> 
> I am attempting to run a PBServer as a win32 service using the
> following code: 
> 
>         import win32serviceutil 
>         import win32service 
>         import win32event 
>         from twisted.spread import pb 
>         from twisted.internet import reactor 
>         import node 
> 
>         class NodeService(win32serviceutil.ServiceFramework): 
>                 _svc_name_ = 'NodeService' 
>                 _svc_display_name_ = 'Node Server' 
> 
>                 def __init__(self, args): 
> 
> win32serviceutil.ServiceFramework.__init__(self, args) 
> 
>                 def SvcDoRun(self): 
>                         n = node.Node() 
>                         reactor.listenTCP(node.port,
> pb.PBServerFactory(n)) 
>                         reactor.run() 
> 
>                 def SvcStop(self): 
> 
> self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING) 
>                         reactor.stop() 
> 
>         if __name__ == '__main__': 
>                 win32serviceutil.HandleCommandLine(NodeService) 
> 
> where my class "node.Node" inherits from pb.Root 
> 
> When I try to start the service it just exits quickly without actually
> starting, but without any error. 

There are a number of issues with this code, e.g. SvcDoRun and SvcStop
are going to be called from a different thread, plus errors will
probably get lost as you are logging to stderr.

Here's some sample code that deals with these issues:
http://svn.twistedmatrix.com/cvs/sandbox/moonfallen/ntsvc/runner.py?view=auto&rev=12804

With one minor bug, it ought to do reactor.callFromThread(reactor.stop)
instead of reactor.stop().






More information about the Twisted-Python mailing list