Help: how to run python applications as NT service?

gene tani gene.tani at gmail.com
Mon Dec 12 12:32:23 EST 2005


zxo102 wrote:
> Hi there,
>     I have a python application (many python scripts) and I start the
> application like this
>
> python myServer.py start
>
> in window. It is running in dos window. Now I would like to put it in
> background as NT service. I got a example code: SmallestService.py from
> chapter 18 of the book "Python Programming On Win32" by Mark Hammond
> etc. The code is as follows and runs well as an NT service.
>
> Since I don't have the book, anybody knows how to "insert" my "python
> myServer.py start" into the sample code as follows.
>
> Thanks a lot for your help.
>
> ouyang
>
> #####################################################
> import win32serviceutil
> import win32service
> import win32event
>
> class SmallestPythonService(win32serviceutil.ServiceFramework):
>     _svc_name_ = "SmallestPythonService"
>     _svc_display_name_ = "The smallest possible Python Service"
>     def __init__(self, args):
>         win32serviceutil.ServiceFramework.__init__(self, args)
>         # Create an event which we will use to wait on.
>         # The "service stop" request will set this event.
>         self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)
>
>     def SvcStop(self):
>         # Before we do anything, tell the SCM we are starting the stop
> process.
>         self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
>         # And set my event.
>         win32event.SetEvent(self.hWaitStop)
>
>     def SvcDoRun(self):
>         # We do nothing other than wait to be stopped!
>         win32event.WaitForSingleObject(self.hWaitStop,
> win32event.INFINITE)
>
> if __name__=='__main__':
>     win32serviceutil.HandleCommandLine(SmallestPythonService)

look at ASPN activestate (online cookbook) recipes linked in this
thread (sorry i don't do those qURL things)

http://groups.google.com/group/comp.lang.python/browse_frm/thread/39e482a3ea7f6041/8ac679d13cf95867?q=windows+service+activestate&rnum=1#8ac679d13cf95867




More information about the Python-list mailing list