最近在解決定時任務問題找到了apscheduler模塊,貼一段代碼
from apscheduler.schedulers.blocking import BlockingScheduler
import time
def tick():
print('Tick! The time is: %s' % datetime.now())
while True:
time.sleep(3)
print "11111"
def run():
#調用了 APScheduler 模塊
scheduler = BlockingScheduler()
scheduler.add_job(tick,'interval',seconds=3) #tick也可以傳參數,3秒執行tick函數
try:
scheduler.start()
except (KeyboardInterrupt, SystemExit):
scheduler.shutdown()
run()
當程序運行時會報錯,警告
WARNING:apscheduler.scheduler:Execution of job "time_tick.tick (trigger: interval[0:00:03], next run at: 2016-11-01 15:25:48 CST)" skipped: maximum number of running instances reached (1)
翻譯大概是,運行的實例大於了1
問題分析:
tick函數使用while 出現了阻塞,第一次執行還沒結束成阻塞狀態,下一個3秒又要執行tick函數,導致的