先看一个demo来复现下这个错误:
from apscheduler.schedulers.blocking import BlockingScheduler import time from threading import Timer from datetime import datetime def test1(who): print("hello") print(datetime.now()) time.sleep(20) print("this is %s" %who) print(datetime.now()) def scheduler_test(): scheduler = BlockingScheduler() i=0 while i<1: scheduler.add_job(test1, 'interval', seconds=10, id='test_job'+str(i), args=["xiao"+str(i)],next_run_time=datetime.now()) i=i+1 print("mmmmmmm") scheduler.start() def timer1(who): who_tmp=who test1(who) Timer(20, timer_test, kwargs={'who': who_tmp}).start() def timer_test(): i=0 while i<3: timer1("xiao"+str(i)) if __name__ == '__main__': #timer_test() scheduler_test() print("when")
可以看到interval时长小于job的执行时长时,会触发这个错误maximum number of running instances reached
解决办法有两个,1、修改add_job接口的interval时长,使之大于job的执行时长 2、add_job接口加个参数,比如 max_instances=20,调大允许的并发个数