免费视频淫片aa毛片_日韩高清在线亚洲专区vr_日韩大片免费观看视频播放_亚洲欧美国产精品完整版

打開APP
userphoto
未登錄

開通VIP,暢享免費電子書等14項超值服

開通VIP
Python 編寫Windows服務程序:將Python作為Windows服務啟動

Python程序作為Windows服務啟動,需要安裝pywin32包。下載路徑:

http://sourceforge.net/projects/pywin32/files/pywin32/


 


#-*- coding:utf-8 -*-

  1. import win32serviceutil   
  2. import win32service   
  3. import win32event   
  4.   
  5. class PythonService(win32serviceutil.ServiceFramework):   
  6.     """ 
  7.     Usage: 'PythonService.py [options] install|update|remove|start [...]|stop|restart [...]|debug [...]' 
  8.     Options for 'install' and 'update' commands only: 
  9.      --username domain\username : The Username the service is to run under 
  10.      --password password : The password for the username 
  11.      --startup [manual|auto|disabled|delayed] : How the service starts, default = manual 
  12.      --interactive : Allow the service to interact with the desktop. 
  13.      --perfmonini file: .ini file to use for registering performance monitor data 
  14.      --perfmondll file: .dll file to use when querying the service for 
  15.        performance data, default = perfmondata.dll 
  16.     Options for 'start' and 'stop' commands only: 
  17.      --wait seconds: Wait for the service to actually start or stop. 
  18.                      If you specify --wait with the 'stop' option, the service 
  19.                      and all dependent services will be stopped, each waiting 
  20.                      the specified period. 
  21.     """  
  22.     #服務名   
  23.     _svc_name_ = "PythonService"  
  24.     #服務顯示名稱   
  25.     _svc_display_name_ = "Python Service Demo"  
  26.     #服務描述   
  27.     _svc_description_ = "Python service demo."  
  28.   
  29.     def __init__(self, args):   
  30.         win32serviceutil.ServiceFramework.__init__(self, args)   
  31.         self.hWaitStop = win32event.CreateEvent(None00None)  
  32.         self.logger = self._getLogger()  
  33.         self.isAlive = True  
  34.           
  35.     def _getLogger(self):  
  36.         import logging  
  37.         import os  
  38.         import inspect  
  39.           
  40.         logger = logging.getLogger('[PythonService]')  
  41.           
  42.         this_file = inspect.getfile(inspect.currentframe())  
  43.         dirpath = os.path.abspath(os.path.dirname(this_file))  
  44.         handler = logging.FileHandler(os.path.join(dirpath, "service.log"))  
  45.           
  46.         formatter = logging.Formatter('%(asctime)s %(name)-12s %(levelname)-8s %(message)s')  
  47.         handler.setFormatter(formatter)  
  48.           
  49.         logger.addHandler(handler)  
  50.         logger.setLevel(logging.INFO)  
  51.           
  52.         return logger  
  53.   
  54.     def SvcDoRun(self):  
  55.         import time  
  56.         self.logger.error("svc do run....")   
  57.         while self.isAlive:  
  58.             self.logger.error("I am alive.")  
  59.             time.sleep(1)  
  60.         # 等待服務被停止    
  61.         #win32event.WaitForSingleObject(self.hWaitStop, win32event.INFINITE)    
  62.               
  63.     def SvcStop(self):   
  64.         # 先告訴SCM停止這個過程    
  65.         self.logger.error("svc do stop....")  
  66.         self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)   
  67.         # 設置事件    
  68.         win32event.SetEvent(self.hWaitStop)   
  69.         self.isAlive = False  
  70.   
  71. if __name__=='__main__':   
  72.     win32serviceutil.HandleCommandLine(PythonService)  


安裝服務

python PythonService.py install

讓服務自動啟動

python PythonService.py --startup auto install 

啟動服務

python PythonService.py start

重啟服務

python PythonService.py restart

停止服務

python PythonService.py stop

刪除/卸載服務

python PythonService.py remove


本站僅提供存儲服務,所有內容均由用戶發(fā)布,如發(fā)現(xiàn)有害或侵權內容,請點擊舉報。
打開APP,閱讀全文并永久保存 查看更多類似文章
猜你喜歡
類似文章
#102 (Support running Twisted code as NT serv...
使用Python編寫免安裝運行時、以Windows后臺服務形式運行的WEB服務器
Create win32 services using Python and py2exe...
python 格式化日志輸出
Python 代碼調試二三事
詳解python的幾種標準輸出重定向方式
更多類似文章 >>
生活服務
分享 收藏 導長圖 關注 下載文章
綁定賬號成功
后續(xù)可登錄賬號暢享VIP特權!
如果VIP功能使用有故障,
可點擊這里聯(lián)系客服!

聯(lián)系客服