python 定时器功能的实现方法

Python主要有三种数据类型:字典、列表、元组。其分别由花括号,中括号,小括号表示。
如:
字典:dic={'a':12,'b':34}
列表:list=[1,2,3,4]
元组:tup=(1,2,3,4)
下面是我写的一个可以执行的例子。
import time
import threading
def threadFunc():
    print 'func() passed to Thread. n times already!'
class TimerThread(threading.Thread):
    def __init__(self, fn, args=(), sleep=1, lastDo = True):
        threading.Thread.__init__(self)
        self.fn = fn
        self.args = args
        self.sleep = sleep
        self.lastDo = lastDo
#        self.setDaemon(True)
        self.isPlay = True
        self.fnPlay = False
    def __do(self):
        self.fnPlay = True
        apply(self.fn, self.args)
        self.fnPlay = False
    def run(self):
        while self.isPlay:
            time.sleep(self.sleep)
            self.__do()
    def stop(self):
        self.isPlay = False
        while True:
            if not self.fnPlay:
                break
            time.sleep(0.01)
        if self.lastDo:
            self.__do()
mytimer = TimerThread(threadFunc, '', 2)
mytimer.start()
#mytimer.join()
while True:
    time.sleep(5)
    print 'this is main thread.'

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值