1.背景
如果你想用Python开发Windows程序,并让其开机启动等,就必须写成windows的服务程序Windows Service,用Python来做这个事情必须要借助第三方模块pywin32,自己去下载然后安装。
2.实例
#encoding=utf-8
importwin32serviceutilimportwin32serviceimportwin32eventimportosimportloggingimportinspectclassPythonService(win32serviceutil.ServiceFramework):
_svc_name_= "PythonService"_svc_display_name_= "jlw Python Service Test"_svc_description_= "这是一段python服务代码"
def __init__(self, args):
win32serviceutil.ServiceFramework.__init__(self, args)
self.hWaitStop=win32event.CreateEvent(None, 0, 0, None)
self.logger=self._getLogger()
self.run=Truedef_getLogger(self):
logger= logging.getLogger('[PythonService]')
this_file=inspect.getfile(inspect.currentframe())
dirpath=os.path.abspath(os.path.dirname(this_file))
handl