Python sched模块
这个模块是用于调度不同action的执行顺序
s=sched.scheduler(time.time,time.sleep)
- class
sched.scheduler(timefunc, delayfunc) -
The
schedulerclass defines a generic interface to scheduling events. It needs two functions to actually deal with the “outside world” — timefunc should be callable without arguments, and return a number (the “time”, in any units whatsoever). The delayfunc function should be callable with one argument, compatible with the output of timefunc, and should delay that many time units. delayfunc will also be called with the argument0after each event is run to allow other threads an opportunity to run in multi-threaded applications.
1 In [62]: s=sched.scheduler(time.time,time.sleep) 2 3 In [63]: def print_time():print "From print_time",time.time() 4 5 In [64]: def print_some_times(): 6 ....: print time.time() 7 ....: s.enter(5,1,print_time,()) 8 ....: s.run() 9 ....: print time.time() 10 ....: 11 12 In [65]: print_some_times() 13 1475360913.65
5秒后再输出 14 From print_time 1475360918.65 15 1475360918.65
如果
s.enter(5,2,other_func,())
s.enter(5,1,print_time,())
那么同一时间,先执行print_time(),再执行other_func()
s.enter(5,1,print_time,())
s.enter(10,1,other_func,())
如果在执行print_time()时,5秒过后,还没有执行完,那么等print_time()执行完后,再执行other_func()
s.run()会阻塞当前线程的执行
可以用
t=threading.Thread(target=s.run)
t.start()
然后如果你想取消sched中的某个action
就可以在后面用
s.cancal(action)

浙公网安备 33010602011771号