python自定义定时器

 1 # -*- coding: utf-8 -*-
 3 """
 4 定时器方法
 5 """
 6 import threading
 7 import time
 8 
 9 
10 class Timer(threading.Thread):
11     def __init__(self, fn, args=(), sleep=0, lastDo=True):
12         threading.Thread.__init__(self)
13         self.fn = fn
14         self.args = args
15         self.sleep = sleep
16         self.lastDo = lastDo
17         self.setDaemon(True)
18 
19         self.isPlay = True
20         self.fnPlay = False
21 
22     def __do(self):
23         self.fnPlay = True
24         self.fn(*self.args)
25         self.fnPlay = False
26 
27     def run(self):
28         while self.isPlay:
29             self.__do()
30             time.sleep(self.sleep)
31 
32     def stop(self):
33         # stop the loop
34         self.isPlay = False
35         while True:
36             if not self.fnPlay:
37                 break
38             time.sleep(0.01)
39         # if lastDo,do it again
40         if self.lastDo:
41             self.__do()

 

转载于:https://www.cnblogs.com/linkong1081/articles/4615774.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值