问题描述
我正在开发一个项目,将 Python APScheduler 作为 Windows 服务运行,并将结果存入文本文件。我可以顺利安装和启动服务。
我尝试了“阻塞”和“后台”调度程序,它们的行为相同。
我尝试将 scheduler.shutdown() 移动到不同的地方。我想将它放在服务停止函数中并让调度程序运行,直到服务收到停止命令,然后服务停止函数将处理关闭调度程序。
也许你能指出我正确的方向?以下是经过清理的代码,以确保您无需重新启动计算机。
import socket
import win32serviceutil
import servicemanager
import win32event
import win32service
from apscheduler.schedulers.blocking import BlockingScheduler
from ApiLoop import ApiPostLoop
from communicate import Connect_device
class SMWinservice(win32serviceutil.ServiceFramework):
_svc_name_ = "EnvisoftFS"
_svc_display_name_ = "EnvisoftFS"
_svc_description_ = "envisoft.com.tr | 2021 All rights reserved."
@classmethod
def parse_command_line(cls):
win32serviceutil.HandleCommandLine(cls)
def __init__(self,args):
win32serviceutil.ServiceFramework.__init__(self,args)
self.hWaitStop = win32event.CreateEvent(None,None)
socket.setdefaulttimeout(60)
self.Api = ApiPostLoop()
self.scheduler = BlockingScheduler()
def SvcStop(self):
self.stop()
self.ReportServiceStatus(win32service.SERVICE_STOP_PENDING)
win32event.SetEvent(self.hWaitStop)
def SvcDoRun(self):
self.start()
self.Api.SendDiagnostic("İstasyon Çevrimiçi")
self.Api.SendDiagnosticWithType("1") #İstasyon Çevrimiçi
try:
self.Api.GetChannelinformationByStationId()
except:pass
servicemanager.LogMsg(servicemanager.EVENTLOG_informatION_TYPE,servicemanager.PYS_SERVICE_STARTED,(self._svc_name_,''))
self.scheduler.add_job(func=self.ConnectDevices,trigger='cron',second='30,59')
self.scheduler.add_job(func=self.ChangeSystemTime,hour='*/3',minute='1',second='20')
self.scheduler.add_job(func=self.ChangeSaisinformation,hour='23',minute='59',second='20')
self.scheduler.add_job(func=self.ChangeChannelinformation,minute='*/59',second='20')
self.Api.SendDiagnosticWithType("5") # Veri Gönderiyor.
self.main()
def start(self):
self.isrunning = True
def stop(self):
self.isrunning = False
def ConnectDevices(self):
try:
Connect_device()
except:pass
def ChangeSystemTime(self):
#Her 3 saatte bir değişecek !
try:
self.Api.GetServerDateTime()
except:pass
def ChangeSaisinformation(self):
#Günde 1 defa
try:
self.Api.GetStationinformation()
except:pass
def ChangeChannelinformation(self):
#Her saat başı
try:
self.Api.GetChannelinformationByStationId()
except:pass
def SendData(self):
#Her 15 dakikada bir
try:
self.Api.SendData()
except:pass
def main(self):
self.scheduler.start()
if __name__ == '__main__':
SMWinservice.parse_command_line()
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)