欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

python程序封裝為win32服務(wù)的方法

 更新時(shí)間:2021年03月07日 06:53:48   作者:kd丹妮兒  
這篇文章主要為大家詳細(xì)介紹了python程序封裝為win32服務(wù)的方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(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)文章

  • Pyramid添加Middleware的方法實(shí)例

    Pyramid添加Middleware的方法實(shí)例

    在pylons的文檔中,有專門講過(guò)如何添加自己的Middleware, 通過(guò)這些Middleware, 我們可以改變輸入和輸出。這也是WSGI(Web Server Gateway Interface)的優(yōu)勢(shì)和精髓所在,那么在pyramid中,我們?nèi)绾翁砑覯iddleware呢
    2013-11-11
  • 簡(jiǎn)單介紹Python中用于求最小值的min()方法

    簡(jiǎn)單介紹Python中用于求最小值的min()方法

    這篇文章主要介紹了簡(jiǎn)單介紹Python中用于求最小值的min()方法,是Python入門中的基礎(chǔ)知識(shí),需要的朋友可以參考下
    2015-05-05
  • python遍歷小寫英文字母的方法

    python遍歷小寫英文字母的方法

    今天小編就為大家分享一篇python遍歷小寫英文字母的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2019-01-01
  • Python cookbook(數(shù)據(jù)結(jié)構(gòu)與算法)保存最后N個(gè)元素的方法

    Python cookbook(數(shù)據(jù)結(jié)構(gòu)與算法)保存最后N個(gè)元素的方法

    這篇文章主要介紹了Python數(shù)據(jù)結(jié)構(gòu)與算法 保存最后N個(gè)元素的方法,涉及Python基于迭代器與生成器實(shí)現(xiàn)歷史記錄功能的相關(guān)操作技巧,需要的朋友可以參考下
    2018-02-02
  • python進(jìn)程管理工具supervisor使用實(shí)例

    python進(jìn)程管理工具supervisor使用實(shí)例

    這篇文章主要介紹了python進(jìn)程管理工具supervisor使用實(shí)例,本文介紹了supervisor的安裝、配置、使用等內(nèi)容,需要的朋友可以參考下
    2014-09-09
  • Python實(shí)現(xiàn)word2Vec model過(guò)程解析

    Python實(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ù)

    這篇文章主要介紹了使用 Celery Once 來(lái)防止 Celery 重復(fù)執(zhí)行同一個(gè)任務(wù),本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-10-10
  • python讀取txt文件并逐行輸出字符串

    python讀取txt文件并逐行輸出字符串

    Python提供了簡(jiǎn)單且方便的方法來(lái)讀取txt文件,使用open()函數(shù)和readlines()方法逐行輸出文件中的字符串內(nèi)容,我們可以輕松地讀取文件內(nèi)容,并通過(guò)循環(huán)遍歷的方式逐行處理,讀取txt文件的方法在各種應(yīng)用場(chǎng)景中非常常見(jiàn),可以用于數(shù)據(jù)分析、文本處理、日志分析等
    2023-10-10
  • 詳談python在windows中的文件路徑問(wèn)題

    詳談python在windows中的文件路徑問(wèn)題

    下面小編就為大家分享一篇詳談python在windows中的文件路徑問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-04-04
  • Python報(bào)錯(cuò)ValueError:?cannot?convert?float?NaN?to?integer的解決方法

    Python報(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

最新評(píng)論