[Twisted-Python] NT Service
Bob Ippolito
bob at redivi.com
Wed Mar 5 13:41:24 EST 2003
On Wednesday, Mar 5, 2003, at 13:15 America/New_York, Thad Jacobs wrote:
> Hello All,
>
> I've created a module that makes COM calls to our non-sql database
> using
> win32com, and converts the results to XML format (through simple string
> concatenations). I've created a rpy script that calls this module, and
> returns the XML to the browser.
>
> I am currently running the HTTP server per the introduction using the
> command line.
>
> My question:
>
> How should I best deploy this application into a production
> environment? It
> will be running on an Windows 2000 server, and needs to automatically
> load
> and run before anyone logs on in the machine. Is there a way I can
> deploy
> Twisted Web as an NT service? Are there other web servers (besides
> IIS)
> that can be configured to run my rpy script and still allow me to
> utilize
> win32com in my class module?
I'm assuming you've already RTFM'ed about win32all's win32service and
Twisted. Here's a stripped example of what has worked very well for me
for the few times I was forced to deploy under Windows:
import win32serviceutil
import win32service
import win32event
class MyTwistedService(win32serviceutil.ServiceFramework):
_svc_name_ = "MyTwistedService"
_svc_display_name_ = "Example Twisted NT Service"
def __init__(self, args):
win32serviceutil.ServiceFramework.__init__(self, args)
self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)
def SvcStop(self):
self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
win32event.SetEvent(self.hWaitStop)
def SvcDoRun(self):
from twisted.python.log import startLogging
# we don't have the console, so we log somewhere
# the NT service logging facilities probably isn't the right place
startLogging(open('c:/twistedexample.log','a'))
self.CheckForQuit()
startTwisted()
def CheckForQuit(self):
#print "Check For Quit"
rv = win32event.WaitForSingleObject(self.hWaitStop, 10.0)
if not rv == win32event.WAIT_TIMEOUT:
print "Received Quit from Win32"
reactor.stop()
reactor.callLater(1.0, self.CheckForQuit)
def startTwisted():
from twisted.internet.app import Application
app = Application("MyExampleTwistedService")
app.run(save=0)
print "Finished"
if __name__=='__main__':
# you could call startTwisted to run as not-a-service
# or use it for cross-platform reasons
win32serviceutil.HandleCommandLine(MyTwistedService)
More information about the Twisted-Python
mailing list