Search code examples
pythonapscheduler

APScheduler opens more threads


[Python 3.5.2, APScheduler 3.3.1]

APScheduler starts a number of threads and I would like to know why.

This is the code I'm executing (in PyCharm, where I can also graph the threads):

from apscheduler.schedulers.background import BackgroundScheduler
import time

def process_to_execute():
    time.sleep(0.5)

scheduler = BackgroundScheduler()
scheduler.add_job(process_to_execute, 'cron', second="*/1")
scheduler.start()

while True:
    time.sleep(1)

The threads graph is: threads_graph

Why is APScheduler creating so many threads when one would be enough?

Thread-7 is created at 1.6 sec. and its task finishes at 2.1 sec. A new task is executed at 2.6 sec: instead of using immediately Thread-7, Thread-8 is created but the task is executed in Thread-7, leaving thread-8 empty... Is there a reason for this?

The number of scheduler's threads is limited to 10.


Solution

  • APScheduler uses the standard library's concurrent.futures.ThreadPoolExecutor which is kinda lazy about this. That's why.