私はPythonを使用して作成した簡単なWindowsサービスを持っています。私の問題は、サービスの完了までにどれくらいの時間がかかるかわからないということです。データの処理には15秒かかり、4時間以上かかることがあります。 4時間以上はまれですが、これが起こる状況があります。長時間実行されるプロセス(Python)のタイムアウトとWindowsサービス
以下は、Windowsサービスで私が従ってきた一般的なパターンです。私はすべてのロジックを取り出しましたが、それは問題ではなく、ダミーのロギングコマンドを残しました。タイムアウトを使用するのではなく、ロジック部分が完了するまで、サービスが継続しているかどうかをリフレッシュしないようにする方法はありますか?
import win32service
import win32serviceutil
import win32api
import win32con
import win32event
import win32evtlogutil
import os
import sys
import time
import logging
class aservice(win32serviceutil.ServiceFramework):
_svc_name_ = "WeatherService"
_svc_display_name_ = "Weather Service"
_svc_description_ = "Downloads weather data from NOAA and creates maps"
def __init__(self, args):
win32serviceutil.ServiceFramework.__init__(self, args)
self.hWaitStop = win32event.CreateEvent(None, 0, 0, None)
def SvcStop(self):
self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
win32event.SetEvent(self.hWaitStop)
def SvcDoRun(self):
import servicemanager
servicemanager.LogMsg(servicemanager.EVENTLOG_INFORMATION_TYPE,
servicemanager.PYS_SERVICE_STARTED,(self._svc_name_, ''))
self.timeout = 640000 #640 seconds/10 minutes (value is in milliseconds)
#self.timeout = 120000 #120 seconds/2 minutes
# This is how long the service will wait to run/refresh itself (see script below)
while 1:
# Wait for service stop signal, if I timeout, loop again
rc = win32event.WaitForSingleObject(self.hWaitStop, self.timeout)
# Check to see if self.hWaitStop happened
if rc == win32event.WAIT_OBJECT_0:
# Stop signal encountered
servicemanager.LogInfoMsg(self._svc_name_ + " - STOPPED!") #For Event Log
break
else:
#[actual service code between rests]
try:
logging.basicConfig(filename=r"c:\temp\example.log",level=logging.DEBUG,
format='%(asctime)s %(message)s')
logging.debug('This message should go to the log file')
logging.info('So should this')
logging.warning('And this, too')
#file_path = "C:\whereever\my_REAL_py_work_to_be_done.py"
#execfile(file_path) #Execute the script
#inc_file_path2 = "C:\whereever\MORE_REAL_py_work_to_be_done.py"
#execfile(inc_file_path2) #Execute the script
except:
pass
#[actual service code between rests]
def ctrlHandler(ctrlType):
return True
if __name__ == '__main__':
win32api.SetConsoleCtrlHandler(ctrlHandler, True)
win32serviceutil.HandleCommandLine(aservice)
ありがとうございます。ですから、無限の時間を取るために停止信号/ループをどのように作りますか? –