if you want to use Python to develop Windows program, and let the other boot, you must write windows service program Windows Service, using Python to do this thing must be with the third party module pywin32, to download and install (note download in line with their own version of OS).


1) sample analysis. Fortunately there is a simple service template, sufficient to meet the requirements of most people:

 #encoding=utf-8 #ZPF import win32serviceutil import win32service import win32event class PythonService (win32serviceutil.ServiceFramework): # service _svc_name_ = "PythonService" # display service in Windows system _svc_display_name_ name = "Python Service Test" # service Description _svc_description_ = "This code is a Python service Test def __init__ (self, args): win32serviceutil.ServiceFramework.__init__ (self, args) self.hWaitStop = win32event.CreateEvent (None, 0, 0, None) def SvcDoRun (self): # put their code into here, OK # waiting service The service is stopped win32event.WaitForSingleObject (self.hWaitStop, win32event.INFINITE) def SvcStop (self): # told SCM to stop the process of self.ReportServiceStatus (win32service.SERVICE_STOP_PENDING) win32event.SetEvent (self.hWaitStop) # set event: win32serviceutil.HandleCommandLine if __name__=='__main__'(PythonService) # brackets parameters can be changed to other names, but must be consistent with the class class name; 

2) explain this. Code: __init__ function in PythonService after the implementation, the system service started, windows system will automatically call the SvcDoRun function, this function of the executive can not end, because the end stop on behalf of service. So when we put their code execution in the SvcDoRun function, you must ensure that the function does not exit or exit, if the function is not normal operation means to stop the service, the system will prompt:

3). When the service is stopped, the system will call the SvcDoStop function, the SvcDoRun function allows the function exits by setting the flag and so on, is the normal stop service. In the example, the function is stopped by stopping the function by stopping the function by stopping the SvcDoRun function from waiting through the event event.

4). Note: the system does not call the SvcDoStop function when the system is shut down, so this service can be set to boot from the start.

2. examples of

are generally set by the SvcDoRun function to achieve the cycle does not exit to see examples to achieve by setting the flag:

 #ZPF #encoding=utf-8 import run win32serviceutil import win32service import win32event import OS import logging import inspect class PythonService (win32serviceutil.ServiceFramework): _svc_name_ = "PythonService _svc_display_name_ =" Python Service "" Test _svc_description_ = "This is a python service test code def __init__ (self, args): win32serviceutil.ServiceFramework.__init__ (self, args) self.hWaitStop = win32event.CreateEvent (None, 0, 0, None) (self.logger = self._getLogger) self.run = True def _getLogger (self): Logger = logging.getLogger ('[PythonService]') this_file (inspect.currentframe) = inspect.getfile (dirpath) = os.path.abspath (os.path.dirname (this_file) handler (os.path.join) = logging.FileHandler (dirpath, service.log) = (formatter) logging.Formatter' (asctime) s% (name) -12s% (levelname) -8s% (message) s'handler.setFormatter (formatter) (logger.addHandler) handler) logger.setLevel (logging.INFO) return logger def SvcDoRun (self): import time self.logger.info ("service is run... While self.run:.") self.logger.info ("I am runing...") time.sleep (2) def SvcStop (self): self.logger.info ("service is stop... Self.ReportServiceStatus (win32service.SERVICE_STOP_PENDING.) Win32) Event.SetEvent (self.hWaitStop) self.run = False if __name__=='__main__'win32serviceutil.HandleCommandLine (PythonService) 


the following command service operation is the basic of the service operation command:

1. installed

 service Python PythonService.py install 

2. to start automatically service

 Python PythonService.py --startup auto install 

3. to start the service

 Python PythonService.py start 


 Python PythonService.py to restart the service restart 


 to stop the service Python PythonService.py stop 

6. to remove / uninstall service

 Python PythonService.py remove 

" P> is the whole content of this article, I hope to help you, and hope that you can support a lot of scripting home.

This concludes the body part