日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時(shí)間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
windowsservice運(yùn)行Python相關(guān)操作技巧分享

我們今天將要為大家介紹的是有關(guān)windows service運(yùn)行Python的一些應(yīng)用技巧。相信用過Python這一編程語言的朋友們都會(huì)發(fā)現(xiàn)這一門功能強(qiáng)大,簡(jiǎn)單易用的程序語言,可以幫助我們輕松的實(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)這本書里找到了相關(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名字一致;

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

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


新聞名稱:windowsservice運(yùn)行Python相關(guān)操作技巧分享
URL標(biāo)題:http://www.5511xx.com/article/dpopiop.html