sched模块提供了一个用于事件调度的类。
>>> import sched, time >>> s = sched.scheduler(time.time, time.sleep) >>> def print_time(a='default'): print("From print_time", time.time(), a) >>> def print_some_times(): print(time.time()) s.enter(10, 1, print_time) s.enter(5, 2, print_time, argument=('positional',)) s.enter(5, 1, print_time, kwargs={'a': 'keyword'}) s.run() print(time.time()) >>> print_some_times() 930343690.257 From print_time 930343695.274 positional From print_time 930343695.275 keyword From print_time 930343700.273 default 930343700.276
>>> import sched, time >>> s = sched.scheduler(time.time, time.sleep) >>> def print_time(a='default'): print("From print_time", time.time(), a) >>> def print_some_times(): print(time.time()) s.enter(10, 1, print_time) s.enter(5, 2, print_time, argument=('positional',)) s.enter(5, 1, print_time, kwargs={'a': 'keyword'}) print("Next : ",s.run(False)) print(time.time())第一次调用:
>>> print_some_times() 1435115632.601069 Next : 5.0 1435115632.656073Next表示下一个事件将在5秒后执行。第二次超过10秒后调用:
>>> print_some_times() 1435115665.549954 From print_time 1435115665.596957 keyword From print_time 1435115665.607957 positional From print_time 1435115665.618958 default Next : 4.966997861862183 1435115665.635959这时事件已经全部达到执行时间点,所以全部立即执行。
原文地址:http://blog.csdn.net/tomato__/article/details/46620517