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) >
Basically you insert your python program code inside the SvcDoRun method of this service. You will, of course, have to change it to act like class method code instead of regular main program code. You can then register it and start/stop it like any other service. The other part you add is code about how often you want the service to wake up and run the SvcDoRun method. Normally you wouldn't use win32event.INFINITE but rather wait some number of milliseconds before running a second time. The example just waits for a stop signal. Here is a skeleton of a service that should get you started. I HIGHLY recommend spending the money to purchase the book (Python Programming on WIN32). There are more extensive examples than the one you have that would help you a lot. Hope this helps. Larry Bates import win32serviceutil import win32service import win32event import win32evtlogutil class Yourservice(win32serviceutil.ServiceFramework): # Indent all code below 4 spaces def SvcDoRun(self): import servicemanager #------------------------------------------------------ # Make entry in the event log that this service started #------------------------------------------------------ servicemanager.LogMsg(servicemanager.EVENTLOG_INFORMATION_TYPE, servicemanager.PYS_SERVICE_STARTED, (self._svc_name_, '')) #------------------------------------------------------------- # Set an amount of time to wait (in milliseconds) between runs #------------------------------------------------------------- self.timeout=60000 (60 seconds) while 1: #------------------------------------------------------- # Wait for service stop signal, if I timeout, loop again #------------------------------------------------------- rc=win32event.WaitForSingleObject(self.hWaitStop, self.timeout) # # Check to see if self.hWaitStop happened # if rc == win32event.WAIT_OBJECT_0: # # Stop signal encountered # break else: # # Put your code here # # # Only return from SvcDoRun when you wish to stop # return def SvcStop(self): #--------------------------------------------------------------------- # Before we do anything, tell SCM we are starting the stop process. #--------------------------------------------------------------------- self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING) #--------------------------------------------------------------------- # And set my event #--------------------------------------------------------------------- win32event.SetEvent(self.hWaitStop) return -- http://mail.python.org/mailman/listinfo/python-list