av激情亚洲男人的天堂国语,日韩欧美精品一中文字幕,无码av一区二区三区无码,国产又色又爽又刺激的a片,国产又色又爽又刺激的a片

windowsservice運行Python相關(guān)操作技巧分享

我們今天將要為大家介紹的是有關(guān)windows service運行Python的一些應(yīng)用技巧。相信用過Python這一編程語言的朋友們都會發(fā)現(xiàn)這一門功能強大,簡單易用的程序語言,可以幫助我們輕松的實現(xiàn)許多功能需求。

 
 
 
  1. import wmi
  2. import os
  3. c = wmi.WMI()
  4. watcher = c.Win32_PowerManagementEvent.watch_for
    (EventType=7) # 監(jiān)視待機事件;
  5. while True:
  6. os.system("kdlj.vbs") # 運行“連接寬帶“的程序,
    這里還是用了上次那位仁兄的vbs代碼;
  7. watcher()

由于windows service運行Python的控制臺窗口一直在那兒,看著有點礙事兒。于是乎想到要是能把他以windows service的方式運行,就像其他在windows服務(wù)管理器里的程序一樣。

最終,在"Python Programming On Win32"(by Mark Hammond)這本書里找到了相關(guān)介紹,它里面有一個簡單的模版,把程序代碼放入相應(yīng)位置就可以了:

 
 
 
  1. # SmallestService.py
  2. #
  3. # A sample demonstrating the smallest possible service written in Python.
  4. import win32serviceutil
  5. import win32service
  6. import win32event
  7. class SmallestPythonService(win32serviceutil.ServiceFramework):
  8. _svc_name_ = "SmallestPythonService"
  9. _svc_display_name_ = "The smallest possible Python Service"
  10. def __init__(self, args):
  11. win32serviceutil.ServiceFramework.__init__(self, args)
  12. # Create an event which we will use to wait on.
  13. # The "service stop" request will set this event.
  14. self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)
  15. def SvcStop(self):
  16. # Before we do anything, tell the SCM we are starting the stop process.
  17. self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
  18. # And set my event.
  19. win32event.SetEvent(self.hWaitStop)
  20. def SvcDoRun(self):
  21. # 把你的程序代碼放到這里就OK了
  22. win32event.WaitForSingleObject(self.hWaitStop, win32event.INFINITE)
  23. if __name__=='__main__':
  24. win32serviceutil.HandleCommandLine(SmallestPythonService) 
  25. # 括號里的名字可以改成其他的,必須與class名字一致;

接下來,只要安裝一下服務(wù),cmd下運行:SmallestService.py install 就行了。這樣,你就可以在windows服務(wù)管理器里找到一個名叫"The smallest possible Python Service"的服務(wù)了,設(shè)成自動啟動,就會開機自動啟動并且一直在后臺運行了。(眼不見心不煩,)

不過,這樣雖然達(dá)到windows service運行Python的目的了,但還是發(fā)現(xiàn)個小問題,就是要是想停止該服務(wù),關(guān)閉的進(jìn)度條就愣在那里不動了,必須在進(jìn)程管理器里把pythonservice.exe關(guān)掉才行。


分享標(biāo)題:windowsservice運行Python相關(guān)操作技巧分享
文章URL:http://uogjgqi.cn/article/dpopiop.html
掃二維碼與項目經(jīng)理溝通

我們在微信上24小時期待你的聲音

解答本文疑問/技術(shù)咨詢/運營咨詢/技術(shù)建議/互聯(lián)網(wǎng)交流