监测数据采集物联网应用开发步骤(5.3)
定时器插件化开发
在com.zxy.common.Com_Para.py中添加如下内容
#定时器正在运行标签
bTimeFlag = False
#定时器插件拦截器
TimeREFLECT_IN_CLASS = "com.plugins.usereflect.testCustTimeReflectClass1"
创建自定义定时器执行类com.zxy.taskhandler.TimeTask.py
#! python3
# -*- coding: utf-8 -
'''
Created on 2017年05月10日
@author: zxyong 13738196011
'''
import datetime,importlib,threading,time
from com.zxy.common import Com_Para
from com.zxy.z_debug import z_debug
#监测数据采集物联网应用--自定义定时器
class TimeTask(z_debug):
# rundatetime固定时间执行 delay延迟多少秒一次性执行 Interval定时循环执行sleeps(时间)
def __init__(self, rundatetime, delay, Interval, sleeps, sname):
self.rundatetime = rundatetime
self.delay = delay
self.Interval = Interval
self.sleeps = sleeps
self.sname = sname
# 定时执行程序
def stimedo(self):
Com_Para.bTimeFlag = True
self.runTask()
Com_Para.bTimeFlag = False
#线程调用
def run(self):
t = threading.Thread(target=self.show, name=self.sname)
t.start()
#插件类反射调用
def RunReflectInClass(self):
#定时任务拦截器
if Com_Para.TimeREFLECT_IN_CLASS != "":
try:
objC = importlib.import_module(Com_Para.TimeREFLECT_IN_CLASS) #对模块进行导入
objName = Com_Para.TimeREFLECT_IN_CLASS.split(".")
objN = getattr(objC,objName[len(objName) - 1])
if hasattr(objN,"strContinue"):
#传入参数
#setattr(objN,"strResult",strResult)
fun_us = getattr(objN,"init_start")
fun_us(objN)
#获取参数
#temResult = getattr(objN,"strResult")
#temContinue = getattr(objN,"strContinue")
except Exception as e:
if str(type(self)) == "":
self.debug_in(self,repr(e)+"=>"+str(e.__traceback__.tb_lineno))#打印异常信息
else:
self.debug_in(repr(e)+"=>"+str(e.__traceback__.tb_lineno))#打印异常信息
finally:
pass
#执行任务
def runTask(self):
try:
#定时器插件调用
self.RunReflectInClass()
except Exception as e:
if str(type(self)) == "":
self.debug_in(self,repr(e)+"=>"+str(e.__traceback__.tb_lineno))#打印异常信息
else:
self.debug_in(repr(e)+"=>"+str(e.__traceback__.tb_lineno))#打印异常信息
finally:
pass
def show(self):
starttime = datetime.datetime.now()
#固定时间开始执行
if self.rundatetime != 0 :
while True:
starttime = datetime.datetime.now()
if starttime > self.rundatetime and not Com_Para.bTimeFlag :
self.stimedo()
break
else:
time.sleep(self.sleeps)
#延迟多少秒执行
if self.delay != 0:
endtime = datetime.datetime.now() + datetime.timedelta(seconds=self.delay)
while True:
starttime = datetime.datetime.now()
if starttime >= endtime and not Com_Para.bTimeFlag:
self.stimedo()
break
else:
time.sleep(self.sleeps)
#定时循环执行
if self.Interval != 0:
endtime = starttime + datetime.timedelta(seconds=self.Interval)
while True:
starttime = datetime.datetime.now()
if starttime >= endtime and not Com_Para.bTimeFlag:
if starttime >= (endtime + datetime.timedelta(seconds=self.Interval)):
endtime = endtime + 2*datetime.timedelta(seconds=self.Interval)
else:
endtime = endtime + datetime.timedelta(seconds=self.Interval)
self.stimedo()
elif endtime > (starttime + datetime.timedelta(seconds=10 * self.Interval)):
endtime = datetime.datetime.now() + datetime.timedelta(seconds=self.Interval)
else:
time.sleep(self.sleeps)
创建定时器插件1 com.plugins.usereflect.testCustTimeReflectClass1.py
#! python3
# -*- coding: utf-8 -
'''
Created on 2017年05月10日
@author: zxyong 13738196011
'''
from com.zxy.z_debug import z_debug
from com.zxy.common.Com_Fun import Com_Fun
#监测数据采集物联网应用--定时器插件1
class testCustTimeReflectClass1(z_debug):
#是否继续执行定时器 1:继续执行 0:中断执行
strContinue = "1"
#数据,传入参数
strResult = ""
def __init__(self, params):
pass
def init_start(self):
self.strResult = "定时器1111执行任务数据输出:"+Com_Fun.GetTimeDef()
print(self.strResult)
创建定时器插件2 com.plugins.usereflect.testCustTimeReflectClass2.py
#! python3
# -*- coding: utf-8 -
'''
Created on 2017年05月10日
@author: zxyong 13738196011
'''
from com.zxy.z_debug import z_debug
from com.zxy.common.Com_Fun import Com_Fun
#监测数据采集物联网应用--定时器插件2
class testCustTimeReflectClass2(z_debug):
#是否继续执行定时器 1:继续执行 0:中断执行
strContinue = "1"
#数据,传入参数
strResult = ""
def __init__(self, params):
pass
def init_start(self):
self.strResult = "定时器2222执行任务数据输出:"+Com_Fun.GetTimeDef()
print(self.strResult)
定时器测试案例MonitorDataCmd.py主文件中编写:
from com.zxy.taskhandler.TimeTask import TimeTask
在主文件中定义线程测试类
#定义线程测试类
class ThreadTest(object):
#定义线程更改定时器插件类
def ChangeTimeREFLECT(self):
time.sleep(13)
Com_Para.TimeREFLECT_IN_CLASS = "com.plugins.usereflect.testCustTimeReflectClass2"
if __name__ == '__main__':下编写
#定时器执行案例,定义定时器插件类名
Com_Para.TimeREFLECT_IN_CLASS = "com.plugins.usereflect.testCustTimeReflectClass1"
# #固定时间执行,当前时间延迟25秒
# times = Com_Fun.DateTimeAdd(datetime.datetime.now(),"S",25)
# print("定时器固定时间执行开始:"+Com_Fun.GetTimeDef())
# tt = TimeTask(times, 0, 5, 0.1, "TimeTask")
# tt.run()
# #当前时间延迟15秒
# print("定时器延迟15秒执行开始:"+Com_Fun.GetTimeDef())
# tt = TimeTask(0, 15, 5, 0.1, "TimeTask")
# tt.run()
# print("定时器循环秒执行开始:"+Com_Fun.GetTimeDef())
# tt = TimeTask(0, 0, 5, 0.1, "TimeTask")
# tt.run()
#
# #延迟13秒变更定时器插件试试
# trt = ThreadTest()
# t = threading.Thread(target=trt.ChangeTimeREFLECT, name="zxyong_ChangeTimeREFLECT")
# t.start()
测试请将对应注释取消
固定时间执测试结果1:
延迟时间执行测试结果2:
执行13秒之后变更定时器插件测试结果3: