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()