5
我工作的一個項目的結果去到一個文本文件運行Python APScheduler作爲Windows服務。我可以順利安裝並啓動服務。如何運行一個Windows服務中APScheduler ......我幾乎沒有
我試過一對夫婦的運行服務內調度最常見的和令人沮喪的結果是,當我停止該服務調度程序的線程繼續寫入文本文件的方式。我必須重新啓動計算機才能殺死線程。
我已經嘗試了「阻塞」和「背景」調度和他們的行爲是相同的。
我打過與scheduler.shutdown()移動到不同的地方。我想把它放在服務停止功能中,讓調度程序運行,直到服務收到停止命令,服務停止功能將處理關閉調度程序。
也許你可以點我在正確的方向?這裏的代碼已被清理,以確保您不必重新啓動計算機。
import win32serviceutil
import win32service
import win32event
import servicemanager
import socket
import time
import logging
import configparser
import os
from datetime import datetime
from mysql.connector import errorcode
from apscheduler.schedulers.background import BackgroundScheduler
global FILEPATH
global SERVICE
#Define constants
FILEPATH = os.path.dirname(os.path.realpath(__file__))
SERVICE = 'service.log'
logging.basicConfig(
filename = '%s\\%s' % (FILEPATH, SERVICE),
level = logging.DEBUG,
format = '[Logging Service] %(levelname)-7.7s %(message)s'
)
def hi(text):
logging.info(text)
return
class HelloWorldSvc (win32serviceutil.ServiceFramework):
_svc_name_ = "Logging-Service"
_svc_display_name_ = "Logging Service"
_svc_description_ = "Periodically logs information"
def __init__(self,args):
win32serviceutil.ServiceFramework.__init__(self,args)
self.stop_event = win32event.CreateEvent(None,0,0,None)
socket.setdefaulttimeout(60)
self.stop_requested = False
def SvcStop(self):
self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
win32event.SetEvent(self.stop_event)
logging.info('Stopping service ...')
self.stop_requested = True
def SvcDoRun(self):
servicemanager.LogMsg(
servicemanager.EVENTLOG_INFORMATION_TYPE,
servicemanager.PYS_SERVICE_STARTED,
(self._svc_name_,'')
)
self.main()
def main(self):
logging.info(' ** Starting Logging Operation ** ')
scheduler = BackgroundScheduler()
scheduler.add_job(hi, 'interval', seconds=5, args=['arg text'])
scheduler.start()
time.sleep(15)
scheduler.shutdown()
time.sleep(10)
logging.info('Ended')
return
if __name__ == '__main__':
win32serviceutil.HandleCommandLine(HelloWorldSvc)
能否請你解釋一下?這個去哪了?機器變量是什麼? – ForgottenKahz 2014-12-18 19:16:28