輕量定時任務調度庫:
1、threading.Timer
2、schedule
一、threading.Timer
python自帶,無需安裝
# encoding: UTF-8
import threading
#Timer(定時器)是Thread的派生類,
#用于在指定時間后調用一個方法。
def func():
?????print'hello timer!'
????timer =threading.Timer(5, func)
????timer.start()
func()
注意:用這個方法要避免python默認最大遞歸深度為1000的問題
? ??????????import sys? ? sys.setrecursionlimit(1000000)? 可修改最大遞歸深度
二、schedule
第三方庫,需要額外安裝,pip install?schedule
import scheduleimport time
def job():
????print("I'm working...")
????schedule.every(10).minutes.do(job)
????schedule.every().hour.do(job)
????schedule.every().day.at("10:30").do(job)
????schedule.every(5).to(10).days.do(job)
????schedule.every().monday.do(job)
????schedule.every().wednesday.at("13:15").do(job)
while True:
?????schedule.run_pending()
? ? time.sleep(1)
注意:
1、time.sleep(1),這句話的作用是:釋放資源。如果沒有這句話,就會造成大量的cpu占用
2、任務調度是按順序執行的,所以上一個任務可能會影響到下一個任務,可以改用多線程的方式:? ?https://blog.csdn.net/kamendula/article/details/51452352