自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

以Windows Service的方式運行Python程序

開發(fā) 后端
本文將介紹如何以Windows Service的方式運行Python程序,語句很簡單,要用到Tim Golden編寫的wmi.py 和 Mark Hammond的win32 extensions for Python ,兩個缺一不可。

Python程序代碼

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

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

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

  1. #SmallestService.py  
  2. #  
  3. #AsampledemonstratingthesmallestpossibleservicewritteninPython.  
  4.  
  5. importwin32serviceutil  
  6. importwin32service  
  7. importwin32event  
  8.  
  9. classSmallestPythonService(win32serviceutil.ServiceFramework):  
  10. _svc_name_="SmallestPythonService" 
  11. _svc_display_name_="ThesmallestpossiblePythonService" 
  12. def__init__(self,args):  
  13. win32serviceutil.ServiceFramework.__init__(self,args)  
  14. #Createaneventwhichwewillusetowaiton.  
  15. #The"servicestop"requestwillsetthisevent.  
  16. self.hWaitStop=win32event.CreateEvent(None,0,0,None)  
  17.  
  18. defSvcStop(self):  
  19. #Beforewedoanything,telltheSCMwearestartingthestopprocess.  
  20. self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)  
  21. #Andsetmyevent.  
  22. win32event.SetEvent(self.hWaitStop)  
  23.  
  24. defSvcDoRun(self):
  25. #把你的程序代碼放到這里就OK了
    win32event.WaitForSingleObject(self.hWaitStop,win32event.INFINITE)

    if__name__=='__main__':
    win32serviceutil.HandleCommandLine(SmallestPythonService)
    #括號里的名字可以改成其他的,必須與class名字一致; 

接下來,只要安裝一下服務,cmd下運行:SmallestService.pyinstall就行了。

這樣,你就可以在windows服務管理器里找到一個名叫"ThesmallestpossiblePythonService"的服務了,設成自動啟動,就會開機自動啟動并且一直在后臺運行了。(眼不見心不煩,)

不過,這樣雖然達到目的了,但還是發(fā)現(xiàn)個小問題,就是要是想停止該服務,關(guān)閉的進度條就愣在那里不動了,必須在進程管理器里把pythonservice.exe關(guān)掉才行,這個bug一直沒法解決,就是關(guān)閉服務的同時,要把監(jiān)視待機事件取消,否則退不出這個死循環(huán)。要是哪位高人看到了,希望可以指點一二。

【編輯推薦】

  1. 淺談Python和C#中的Run As代碼實現(xiàn)方式
  2. 腳本語言排行榜 PHP、Ruby和Python領(lǐng)先
  3. Python選擇Mercurial Hg版本控制系統(tǒng)
  4. 在Python中解決中英文混雜出錯的問題
  5. 使用Oracle數(shù)據(jù)庫實現(xiàn)Python數(shù)據(jù)持久
責任編輯:彭凡 來源: cnblogs
相關(guān)推薦

2010-03-04 10:50:45

windows ser

2022-05-13 10:24:44

WineWindowsLinux

2021-12-23 22:48:17

Windows 11Windows微軟

2018-12-21 19:30:28

Windows 10Windows管理員身份

2009-05-28 09:48:21

微軟Windows 7操作系統(tǒng)

2023-10-25 18:18:10

Python腳本代碼

2011-08-17 15:39:16

Windows7管理員身份運行程序

2009-11-27 13:45:02

Tomcat for

2019-12-12 10:46:15

Kubernetes容器系統(tǒng)

2009-03-21 16:32:00

虛擬化VmwareWindows

2010-12-30 10:10:06

NetLinux學習

2009-10-22 08:57:56

2020-02-03 09:53:12

卸載驅(qū)動程序Windows 10

2022-01-04 12:43:47

Haiku操作系統(tǒng)Wine補丁

2010-04-13 08:49:08

JavaLombokJavabean

2022-04-27 09:39:11

Mixin工具

2010-06-18 14:24:47

AODV協(xié)議

2018-06-26 13:05:17

Windows 10pingInternet

2010-02-22 15:06:05

Python解釋程序

2020-01-15 13:50:29

桌面快捷方式Windows 10WiFi網(wǎng)絡
點贊
收藏

51CTO技術(shù)棧公眾號