python程序封裝為win32服務(wù)的方法
本文實(shí)例為大家分享了python程序封裝為win32服務(wù)的具體代碼,供大家參考,具體內(nèi)容如下
# encoding=utf-8 import os import sys import winerror import win32serviceutil import win32service import win32event import servicemanager class PythonService(win32serviceutil.ServiceFramework): # 服務(wù)名 _svc_name_ = "PythonService1" # 服務(wù)顯示名稱 _svc_display_name_ = "PythonServiceDemo" # 服務(wù)描述 _svc_description_ = "Python service demo." def __init__(self, args): win32serviceutil.ServiceFramework.__init__(self, args) self.hWaitStop = win32event.CreateEvent(None, 0, 0, None) self.logger = self._getLogger() self.isAlive = True def _getLogger(self): import logging import os import inspect 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.error("svc do run....") try: while self.isAlive: self.logger.error("I am alive.") time.sleep(1) # 等待服務(wù)被停止 # win32event.WaitForSingleObject(self.hWaitStop, win32event.INFINITE) except Exception as e: self.logger.error(e) time.sleep(60) def SvcStop(self): # 先告訴SCM停止這個(gè)過(guò)程 self.logger.error("svc do stop....") self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING) # 設(shè)置事件 win32event.SetEvent(self.hWaitStop) self.isAlive = False if __name__ == '__main__': if len(sys.argv) == 1: try: src_dll = os.path.abspath(servicemanager.__file__) servicemanager.PrepareToHostSingle(PythonService) servicemanager.Initialize("PythonService", src_dll) servicemanager.StartServiceCtrlDispatcher() except Exception as e: print(e) #if details[0] == winerror.ERROR_FAILED_SERVICE_CONTROLLER_CONNECT: #win32serviceutil.usage() else: win32serviceutil.HandleCommandLine(PythonService) # 參數(shù)和上述定義類名一致 #pip install pywin32 # 安裝服務(wù) # python PythonService.py install # 讓服務(wù)自動(dòng)啟動(dòng) # python PythonService.py --startup auto install # 啟動(dòng)服務(wù) # python PythonService.py start # 重啟服務(wù) # python PythonService.py restart # 停止服務(wù) # python PythonService.py stop # 刪除/卸載服務(wù) # python PythonService.py remove # 在用戶變量處去掉python路徑,然后在環(huán)境變量加入python路徑 # C:\Users\zhongjianhui\AppData\Local\Programs\Python\Python36\Lib\site-packages\pywin32_system32; # C:\Users\zhongjianhui\AppData\Local\Programs\Python\Python36\Lib\site-packages\win32; # C:\Users\zhongjianhui\AppData\Local\Programs\Python\Python36\Scripts\; #C:\Users\zhongjianhui\AppData\Local\Programs\Python\Python36\
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
簡(jiǎn)單介紹Python中用于求最小值的min()方法
這篇文章主要介紹了簡(jiǎn)單介紹Python中用于求最小值的min()方法,是Python入門中的基礎(chǔ)知識(shí),需要的朋友可以參考下2015-05-05Python cookbook(數(shù)據(jù)結(jié)構(gòu)與算法)保存最后N個(gè)元素的方法
這篇文章主要介紹了Python數(shù)據(jù)結(jié)構(gòu)與算法 保存最后N個(gè)元素的方法,涉及Python基于迭代器與生成器實(shí)現(xiàn)歷史記錄功能的相關(guān)操作技巧,需要的朋友可以參考下2018-02-02python進(jìn)程管理工具supervisor使用實(shí)例
這篇文章主要介紹了python進(jìn)程管理工具supervisor使用實(shí)例,本文介紹了supervisor的安裝、配置、使用等內(nèi)容,需要的朋友可以參考下2014-09-09Python實(shí)現(xiàn)word2Vec model過(guò)程解析
這篇文章主要介紹了Python實(shí)現(xiàn)word2Vec model過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-12-12使用 Celery Once 來(lái)防止 Celery 重復(fù)執(zhí)行同一個(gè)任務(wù)
這篇文章主要介紹了使用 Celery Once 來(lái)防止 Celery 重復(fù)執(zhí)行同一個(gè)任務(wù),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-10-10Python報(bào)錯(cuò)ValueError:?cannot?convert?float?NaN?to?intege
在Python編程中,我們經(jīng)常需要處理各種數(shù)據(jù)類型,包括浮點(diǎn)數(shù)和整數(shù),然而,有時(shí)候我們可能會(huì)遇到一些意外的情況,比如將一個(gè)包含NaN(Not?a?Number)的浮點(diǎn)數(shù)轉(zhuǎn)換為整數(shù)時(shí),就會(huì)拋出錯(cuò)誤,本文將探討這個(gè)錯(cuò)誤的原因,并給出幾種可能的解決方案,需要的朋友可以參考下2024-09-09