python的timer带参数传递
2015-10-01 20:07
609 查看
我们经常需要定时的执行某个任务,在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
Timer类则是居然也不是循环使用的,但它的灵活性要差很多,用法也比较简单。
Timer类则是居然也不是循环使用的,但它的灵活性要差很多,用法也比较简单。
如何让Timer循环呢,一种很山寨的方法是……在test_func中再次调用Timer…start…
我再研究一下有没有更优雅的实现……哎。。
Timer类也是一次性触发的,思路和sched大概差不多
除了第三方的模块外,标准库为我们提供了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
123456789101112131415161718 | [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] |
1 2 3 4 5 6 7 8 9 10 11 12 13 | [python] importthreading importtime deftest_func(msg1,msg2): print"I'm test_func,",msg1,msg2 if__name__=="__main__": t=threading.Timer(5,test_func,("msg1","msg2")) t.start() whileTrue: time.sleep(1) [/python] |
12345678910111213 | [python]import threadingimport 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] |
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): "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()
相关文章推荐
- Python连接mysql数据库
- Sublime Text 搭建 Python 开发平台
- 八大排序算法(Python实现)
- virtualenv--python虚拟沙盒的安装使用
- python可变交换性能优化
- python连接数据库设置
- Python 初识GUI
- mp3在ubuntu下修改后显示正常,但回到windows下显示问号。
- python批量下载兰科植物网站的图片,并重命名文件
- numpy 矩阵运算的陷阱
- 如何成为 Python 高手
- nf使用 Python 的 turtle 模块画图
- Python使用模块Pyserial模块报<ImportError:No module named tools.list_ports>
- Python学习笔记<list和tuple>
- Python学习笔记<list和tuple>
- [pythontip]最大非连续子序列
- python捕获多个Exception携带msg
- Python中的除法
- Python核心编程笔记 - 第12章 模块
- 用python编写统计fasta格式的序列的长度脚本