python 结合 schedule 和thread 实现触发式周期调用某函数(更新数据)

发布时间:2014-10-23 23:30:36
来源:分享查询网

#! /usr/bin/env python # This Python file uses the following encoding: utf-8 ''' Created on Nov 18, 2013 @author: root ''' from threading import Thread import time, sched schedule = sched.scheduler(time.time, time.sleep) class test(Thread): time_available_flag=True opt=0 def __init__(self): Thread.__init__(self) def perform_command(self, inc): self.write_xml() def timming_exe(self, inc = 20): schedule.enter(inc, 0, self.perform_command, ( inc,)) schedule.run() def write_xml(self): print('write xml to file opt %s'%test.opt) test.time_available_flag=True print("schedu finished",time.time()) print('some func called here ','opt ------------,写入数据',self.__class__.opt) def run(self): print("schedu started",time.time()) self.timming_exe() def whether_write_xml(self,count): # print(time.time()) if count>0: self.__class__.opt=self.__class__.opt+1 print("time_available_flag",self.__class__.time_available_flag,self.__class__.opt) if self.__class__.time_available_flag: self.__class__.time_available_flag=False print("first start ",self.__class__.time_available_flag) self.start() # print(time.time()) # print(time.time()) return count if __name__ == '__main__': x=test() print(x.whether_write_xml(1)) # time.sleep(2) print(x.whether_write_xml(2)) # time.sleep(3) print(x.whether_write_xml(3)) # time.sleep(5) print(x.time_available_flag) xx=test() print(xx.whether_write_xml(4)) print(xx.whether_write_xml(5))

返回顶部
查看电脑版