Tag Archives: scheduler Error

[Solved] scheduler Error: maximum number of running instances reached

Let’s look at a demo to reproduce this error:

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

You can see that the interval length is less than the execution length of the job, it will trigger this error maximum number of running instances reached

There are two solutions, 1, modify the interval duration of the add_job interface to make it greater than the execution duration of the job 2, add a parameter to the add_job interface, such as max_instances=20, to adjust the number of concurrency allowed.