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

windows service運(yùn)行Python相關(guān)操作技巧分享

開(kāi)發(fā) 后端
在windows service運(yùn)行Python的過(guò)程中,有許多應(yīng)用技巧需要我們及時(shí)的掌握,才能很好的來(lái)操作這一過(guò)程。在這里將會(huì)對(duì)此做一個(gè)詳細(xì)介紹。

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

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

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

最終,在"Python Programming On Win32"(by Mark Hammond)這本書(shū)里找到了相關(guān)介紹,它里面有一個(gè)簡(jiǎ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. # 括號(hào)里的名字可以改成其他的,必須與class名字一致; 

接下來(lái),只要安裝一下服務(wù),cmd下運(yùn)行:SmallestService.py install 就行了。這樣,你就可以在windows服務(wù)管理器里找到一個(gè)名叫"The smallest possible Python Service"的服務(wù)了,設(shè)成自動(dòng)啟動(dòng),就會(huì)開(kāi)機(jī)自動(dòng)啟動(dòng)并且一直在后臺(tái)運(yùn)行了。(眼不見(jiàn)心不煩,)

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

【編輯推薦】

  1. Python下劃線在實(shí)際應(yīng)用中功能體現(xiàn)
  2. 常用Python應(yīng)用技巧內(nèi)容分析
  3. 安裝Python簡(jiǎn)單操作方法分享
  4. Python Helloworld程序簡(jiǎn)單實(shí)現(xiàn)
  5. Python Class正確應(yīng)用代碼示例剖析
責(zé)任編輯:曹凱 來(lái)源: 博客園
相關(guān)推薦

2010-01-25 18:33:35

Android鍵盤(pán)操作

2009-12-10 17:27:39

PHP操作Cookie

2010-01-06 17:02:28

.Net Framew

2010-01-28 14:12:20

Android Act

2009-12-10 16:35:08

PHP操作文章列表

2010-03-03 16:57:28

Python字符

2010-01-13 10:25:30

VB.NET文件夾操作

2010-03-04 14:39:52

Python讀取輸入值

2010-03-03 10:10:33

Python實(shí)現(xiàn)Soc

2010-01-13 15:33:40

VB.NET菜單項(xiàng)目

2009-12-29 16:08:41

Silverlight

2010-02-22 17:58:06

WCF異步上傳

2010-03-03 13:45:08

Python查找重復(fù)文

2009-07-02 16:36:58

Python程序

2010-01-15 15:10:43

VB.NET Stri

2010-02-24 11:22:04

WCF方法重載

2010-01-28 10:55:14

Android電源管理

2009-12-30 13:37:24

Silverlight

2009-12-30 10:25:03

Silverlight

2010-01-04 14:49:30

Silverlight
點(diǎn)贊
收藏

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