• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            隨筆-167  評論-8  文章-0  trackbacks-0
            用python寫windows服務(1)

            以python2.5 為例
            需要軟件

                    * python 2.5
                    * pywin32(與2.5 版本相匹配的)

            Service Control Manager (SCM)

            服務管理器(SCM) 是windows NT的 一部分,所有服務必須通過SCM 注冊,SCM負責啟動,停止服務等。

            當一個進程通過SCM注冊后, 有如下特質:

                    * 運行該進程的用戶,未必是當前登錄的用戶。
                    * 該進程如果依賴其他服務,哪么該服務啟動前,依賴服務回啟動。該服務停止后,依賴服務會停止。(估計是應用計數減1)
                    * 服務可知計算機啟動后自動啟動,或者手動啟動。

            windows NT 通過執行一個進程開始相應服務。一旦這個進程執行,它需要告知SCM它實際上是作為一個服務運行。還需要傳給SCM一個控制句柄(control handler)。其實就是一個函數,用于處理SCM 發來的相關信息。 當服務被停止時, SCM傳信息給控制句柄。服務本身負責處理該請求,并停止本身服務。
            pywin32 服務相關module

                    * win32service 實現了Win32服務功能。
                    * win32serviceutil 對api的包裝,始面向用戶的接口更友好。
                    * PythonService.exe 使用pywin32 服務器,它必須先注冊。

            下面重點講 win32serviceutil
            服務框架類

            win32serviceutil.ServiceFramework

            __init__

                構造函數,注冊ServiceCtrlHandler給SCM

            ServiceCtrlHandler

                本服務的control handler 的默認實現。該函數會查詢類內的函數名,用以判斷該服務提供哪些控制接口,比如類內有SvcPause 函數。則會認為該服務可以被暫停。

            SvcRun

                服務入口點。服務運行,就是運行這個函數。

            簡單示例
            代碼:
             1 # SmallestService.py
             2 #
             3 # A sample demonstrating the smallest possible service written in Python.
             4 
             5 import win32serviceutil
             6 import win32service
             7 import win32event
             8 
             9 class SmallestPythonService(win32serviceutil.ServiceFramework):
            10     _svc_name_ = "SmallestPythonService"
            11     _svc_display_name_ = "The smallest possible Python Service"
            12     def __init__(self, args):
            13         win32serviceutil.ServiceFramework.__init__(self, args)
            14         # Create an event which we will use to wait on.
            15         # The "service stop" request will set this event.
            16         self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)
            17 
            18     def SvcStop(self):
            19         # Before we do anything, tell the SCM we are starting the stop process.
            20         self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
            21         # And set my event.
            22         win32event.SetEvent(self.hWaitStop)
            23 
            24     def SvcDoRun(self):
            25         # We do nothing other than wait to be stopped!
            26         win32event.WaitForSingleObject(self.hWaitStop, win32event.INFINITE)
            27 
            28 if __name__=='__main__':
            29     win32serviceutil.HandleCommandLine(SmallestPythonService)
            30 
            安裝服務
            進入PythonService.exe所在目錄, 命令行下執行命令

            (PATH)>PythonService.exe /register
            Registering the Python Service Manager...
            安裝服務

            C:\Scripts> SmallestService.py install
            Installing service SmallestPythonService to Python class
               C:\Scripts\SmallestService.SmallestPythonService
            Service installed
            C:\Scripts>
            啟動服務

            C:\Scripts> python.exe SmallestService.py start
            Starting service SmallestPythonService
            C:\Scripts>
            啟動確認

            C:\Scripts> python.exe SmallestService.py start
            Starting service SmallestPythonService
            Error starting service: An instance of the service is already running.
            C:\Scripts>
            停止服務

            C:\Scripts> python.exe SmallestService.py stop
            Stopping service SmallestPythonService
            C:\Scripts>


            posted on 2009-08-01 12:55 老馬驛站 閱讀(6307) 評論(0)  編輯 收藏 引用 所屬分類: pythonwindows
            99久久夜色精品国产网站| 久久影院综合精品| 99久久精品免费看国产免费| 中文字幕久久欲求不满| 久久精品国产黑森林| 香蕉久久夜色精品国产尤物| 亚洲精品乱码久久久久久蜜桃不卡 | 99久久无色码中文字幕人妻| 久久亚洲私人国产精品vA | 久久婷婷五月综合国产尤物app | 久久国产V一级毛多内射| 久久人妻AV中文字幕| 夜夜亚洲天天久久| 久久精品国产亚洲AV蜜臀色欲| 久久精品一区二区国产| 性做久久久久久久久老女人| 国内精品伊人久久久影院 | 国产精品免费久久久久影院| 久久久精品国产免大香伊| 久久久久久久尹人综合网亚洲| 久久综合视频网| 久久久WWW免费人成精品| 国产精品久久午夜夜伦鲁鲁| 久久精品国产99久久久古代| 国内精品久久久久国产盗摄| 精品一区二区久久| 久久亚洲欧美国产精品 | 久久国产色AV免费看| 无码人妻少妇久久中文字幕| 狠狠狠色丁香婷婷综合久久五月| 一本一道久久综合狠狠老| 久久久久se色偷偷亚洲精品av| 久久久久久国产精品美女| 久久综合九色综合97_久久久| 久久久久久精品免费看SSS| 一本久久精品一区二区| 中文字幕精品无码久久久久久3D日动漫 | 亚洲午夜久久久影院| 精品国产99久久久久久麻豆| 一本色道久久综合亚洲精品| 久久人人爽人人人人片av|