1、while循环中使用sleep
缺点:不容易控制,而且是个阻塞函数
def timer(n):
''' 每n秒执行一次 '''
while True:
print(time.strftime('%Y-%m-%d %X',time.localtime()))
theTask() # 此处为要执行的任务
time.sleep(n)
2、schedule模块
优点:可以管理和调度多个任务,可以进行控制
缺点:阻塞式函数
import schedule
import time
import datetime
def job1():
print('Job1:每隔10秒执行一次的任务,每次执行2秒')
print('Job1-startTime:%s' %(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')))
time.sleep(2)
print('Job1-endTime:%s' % (datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')))
print('------------------------------------------------------------------------')
def job3():
print('Job3:每隔1分钟执行一次,每次执行10秒')
print('Job3-startTime:%s' % (datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')))
time.sleep(10)
print('Job3-endTime:%s' % (datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')))
print('------------------------------------------------------------------------')
def job4():
print('Job4:每天下午17:49执行一次,每次执行20秒')
print('Job4-startTime:%s' % (datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')))
time.sleep(20)
print('Job4-endTime:%s' % (datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')))
print('------------------------------------------------------------------------')
def job5():
print('Job5:每隔5秒到10秒执行一次,每次执行3秒')
print('Job5-startTime:%s' % (datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')))
time.sleep(3)
print('Job5-endTime:%s' % (datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')))
print('------------------------------------------------------------------------')
if __name__ == '__main__':
schedule.every(10).seconds.do(job1)
schedule.every(1).minutes.do(job3)
schedule.every().day.at('17:49').do(job4)
schedule.every(5).to(10).seconds.do(job5)
while True:
schedule.run_pending()
3、Threading模块中的Timer
优点:非阻塞
缺点:不易管理多个任务
from threading import Timer
import datetime
# 每隔两秒执行一次任务
def printHello():
print('TimeNow:%s' % (datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')))
t = Timer(2, printHello) # 此处使用递归调用实现
t.start()
if __name__ == "__main__":
printHello()
4、sched模块
sched模块实现了一个时间调度程序,该程序可以通过单线程执行来处理按照时间尺度进行调度的时间。
通过调用scheduler.enter(delay,priority,func,args)
函数,可以将一个任务添加到任务队列里面,当指定的时间到了,就会执行任务(func函数)。
- delay:任务的间隔时间。
- priority:如果几个任务被调度到相同的时间执行,将按照priority的增序执行这几个任务。
- func:要执行的任务函数
- args:func的参数
import time, sched
import datetime
s = sched.scheduler(time.time, time.sleep)
def print_time(a='default'):
print('Now Time:',datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'),a)
def print_some_times():
print(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'))
s.enter(10,1,print_time)
s.enter(5,2,print_time,argument=('positional',))
s.run()
print(datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'))
print_some_times()
#执行结果
#2020-04-23 16:25:03
#Now Time: 2020-04-23 16:25:08 positional
#Now Time: 2020-04-23 16:25:13 default
#2020-04-23 16:25:13
#
#Process finished with exit code 0
按顺序执行任务:
import time, sched
import datetime
s = sched.scheduler(time.time, time.sleep)
def event_fun1():
print("func1 Time:", datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'))
def perform1(inc):
s.enter(inc, 0, perform1, (inc,))
event_fun1()
def event_fun2():
print("func2 Time:", datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'))
def perform2(inc):
s.enter(inc, 0, perform2, (inc,))
event_fun2()
def mymain(func, inc=2):
if func == "1":
s.enter(0, 0, perform1, (10,))# 每隔10秒执行一次perform1
if func == "2":
s.enter(0, 0, perform2, (20,))# 每隔20秒执行一次perform2
if __name__ == '__main__':
mymain('1')
mymain('2')
s.run()
# 执行结果
# func1 Time: 2020-04-23 16:30:28
# func2 Time: 2020-04-23 16:30:28
# func1 Time: 2020-04-23 16:30:38
# func2 Time: 2020-04-23 16:30:48
# func1 Time: 2020-04-23 16:30:48
# func1 Time: 2020-04-23 16:30:58
# func2 Time: 2020-04-23 16:31:08
s.run()会阻塞当前线程的执行,可用Thread解决,也可以用s.cancal(action)
来取消sched中的某个action
t=threading.Thread(target=s.run)
t.start()