python的timer带参数传递

我们经常需要定时的执行某个任务,在Linux下我们有强大的crontab,但是在Python这个粒度(定时执行函数),如何处理呢?
除了第三方的模块外,标准库为我们提供了sched模块和Timer类。
先说sched模块,准确的说,它是一个调度(延时处理机制),每次想要定时执行某任务都必须写入一个调度。
使用步骤如下:
(1)生成调度器:
s = sched.scheduler(time.time,time.sleep)
第一个参数是一个可以返回时间戳的函数,第二个参数可以在定时未到达之前阻塞。可以说sched模块设计者是“在下很大的一盘棋”,比如第一个函数可以是自定义的一个函数,不一定是时间戳,第二个也可以是阻塞socket等。
(2)加入调度事件
其实有enter、enterabs等等,我们以enter为例子。
s.enter(x1,x2,x3,x4)
四个参数分别为:间隔事件、优先级(用于同时间到达的两个事件同时执行时定序)、被调用触发的函数,给他的参数(注意:一定要以tuple给如,如果只有一个参数就(xx,))
(3)运行
s.run()
注意sched模块不是循环的,一次调度被执行后就Over了,如果想再执行,请再次enter
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
[ python ]
import time , sched
#被调度触发的函数
def event_func ( msg ) :
print "Current Time:" , time . time ( ) , 'msg:' , msg
if __name__ == "__main__" :
#初始化sched模块的scheduler类
s = sched . scheduler ( time . time , time . sleep )
#设置两个调度
s . enter ( 1 , 2 , event_func , ( "Small event." , ) )
s . enter ( 2 , 1 , event_func , ( "Big event." , ) )
s . run ( )
while True :
time . sleep ( 100 )
[ / python ]
|
Timer类则是居然也不是循环使用的,但它的灵活性要差很多,用法也比较简单。
1
2
3
4
5
6
7
8
9
10
11
12
13
|
[ python ]
import threading
import time
def test_func ( msg1 , msg2 ) :
print "I'm test_func," , msg1 , msg2
if __name__ == "__main__" :
t = threading . Timer ( 5 , test_func , ( "msg1" , "msg2" ) )
t . start ( )
while True :
time . sleep ( 1 )
[ / python ]
|
Timer类则是居然也不是循环使用的,但它的灵活性要差很多,用法也比较简单。
1
2
3
4
5
6
7
8
9
10
11
12
13
|
[ python ]
import threading
import time
def test_func ( msg1 , msg2 ) :
print "I'm test_func," , msg1 , msg2
if __name__ == "__main__" :
t = threading . Timer ( 5 , test_func , ( "msg1" , "msg2" ) )
t . start ( )
while True :
time . sleep ( 1 )
[ / python ]
|
如何让Timer循环呢,一种很山寨的方法是……在test_func中再次调用Timer…start…
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
[ python ]
import threading
import time
def timer_start ( ) :
t = threading . Timer ( 5 , test_func , ( "msg1" , "msg2" ) )
t . start ( )
def test_func ( msg1 , msg2 ) :
print "I'm test_func," , msg1 , msg2
timer_start ( )
if __name__ == "__main__" :
timer_start ( )
while True :
time . sleep ( 1 )
[ / python ]
|
我再研究一下有没有更优雅的实现……哎。。
Timer类也是一次性触发的,思路和sched大概差不多
'''
Created on 2013-7-31
@author: Eric
'''
import time
import threading
def timer_start():
t = threading.Timer(1, test_func, ("Parameter1",))
t.start()
def test_func(msg1):
print("I'm test_func,", msg1)
timer_start()
def timer2():
timer_start()
while True:
time.sleep(1)
if __name__ == "__main__":
timer2()




更多推荐
所有评论(0)