首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > perl python >

python定时器,该怎么解决

2013-01-21 
python定时器每个一定时间,就执行一次操作[解决办法]临时写了个,可用返回来的函数控制停止操作。from threa

python定时器
每个一定时间,就执行一次操作
[解决办法]
临时写了个,可用返回来的函数控制停止操作。

from threading import Timer
def executeEvery(seconds,callback):
    def f():
        callback()
        t = Timer(seconds,f)
        t.start()
    def stop():
        t.cancel()
        
    t = Timer(seconds,f)
    t.start()
    
    return stop    
        
def printHello():
    print('hello')
    
executeEvery(2,printHello)

热点排行