国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

Python編寫Windows Service服務程序

2020-02-16 11:27:40
字體:
來源:轉載
供稿:網友

 如果你想用Python開發Windows程序,并讓其開機啟動等,就必須寫成windows的服務程序Windows Service,用Python來做這個事情必須要借助第三方模塊pywin32,自己去下載然后安裝(注意下載符合自己OS的版本)。

1.示例分析

1).幸運的是這里有一個簡單的服務模版,足以滿足大多數人的要求:

#encoding=utf-8 #ZPF import win32serviceutil import win32service import win32event  class PythonService(win32serviceutil.ServiceFramework):  #服務名  _svc_name_ = "PythonService"  #服務在windows系統中顯示的名稱  _svc_display_name_ = "Python Service Test"  #服務的描述  _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):   # 把自己的代碼放到這里,就OK   # 等待服務被停止   win32event.WaitForSingleObject(self.hWaitStop, win32event.INFINITE)      def SvcStop(self):   # 先告訴SCM停止這個過程   self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)   # 設置事件   win32event.SetEvent(self.hWaitStop)  if __name__=='__main__':  win32serviceutil.HandleCommandLine(PythonService)  #括號里參數可以改成其他名字,但是必須與class類名一致; 

2).解釋一下這段代碼:在類PythonService的__init__函數執行完后,系統服務開始啟動,windows系統會自動調用SvcDoRun函數,這個函數的執行不可以結束,因為結束就代表服務停止。所以當我們放自己的代碼在SvcDoRun函數中執行的時候,必須確保該函數不退出,如果退出或者該函數沒有正常運行就表示服務停止,windows系統會提示:

3).當停止服務的時候,系統會調用SvcDoStop函數,該函數通過設置標志位等方式讓SvcDoRun函數退出,就是正常的停止服務。例子中是通過event事件讓SvcDoRun函數停止等待,從而退出該函數,從而使服務停止。

4).注意:系統關機時不會調用SvcDoStop函數,所以這種服務是可以設置為開機自啟的。

2.實例

一般都是通過在SvcDoRun函數中設置循環來達到不退出的目的,看例子通過設置標志位run來實現:

#ZPF #encoding=utf-8 import 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.getfile(inspect.currentframe())   dirpath = os.path.abspath(os.path.dirname(this_file))   handler = logging.FileHandler(os.path.join(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)   win32event.SetEvent(self.hWaitStop)   self.run = False  if __name__=='__main__':  win32serviceutil.HandleCommandLine(PythonService)             
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 中江县| 界首市| 太仆寺旗| 永吉县| 木里| 汉阴县| 山阳县| 四平市| 即墨市| 宣威市| 丰原市| 固安县| 饶阳县| 溆浦县| 西畴县| 柞水县| 奉贤区| 福海县| 施甸县| 固阳县| 威宁| 宜城市| 嘉定区| 青铜峡市| 静宁县| 搜索| 稷山县| 连州市| 诏安县| 确山县| 九江县| 苏尼特左旗| 讷河市| 巩留县| 留坝县| 乐安县| 甘泉县| 郁南县| 鄂州市| 滁州市| 霍州市|