计时器
描述:用来执行定时任务(异步),点个赞不过分吧
# 计时器类(异步) # time: 间隔时间(秒), 毫秒请使用小数(0.1xxxx) # handle: 要进行的异步处理器(名称) # arg: 异步处理器的参数(元组) class setInteval(threading.Thread): def __init__(self, time, handle, arg): threading.Thread.__init__(self) self.time = time self.handle = handle self.arg = arg self.switch = True # True 为计时器开启, False为计时器关闭 self.start() def run(self): while True: if self.switch is True: try: time.sleep(self.time) self.handle(*(self.arg)) except: raise Exception("set interval error") else: break def clear_interval(self): self.switch = False def test(a): print(a) # 异步计时器测试 inteval = setInteval(1, test, ('aaaaaaaaaaaaaaaa',)) time.sleep(10) inteval.clear_interval()