python 学习定时任务apscheduler模块

时间:2021-06-06 07:46:39

最近在解决定时任务问题找到了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函数,导致的