Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

There are two ways to prevent apscheduler from causing the application to exit:

  1. Setting the daemon parameter of the scheduler to False: By default, the daemon parameter of apscheduler is set to True, which means that the scheduler will run in the background as a daemon thread. If you set this parameter to False, the scheduler will run as a normal thread and will not cause the application to exit.
from apscheduler.schedulers.blocking import BlockingScheduler

def my_job():
    print('Hello')

if __name__ == '__main__':
    scheduler = BlockingScheduler(daemon=False)
    scheduler.add_job(my_job, 'interval', seconds=10)
    scheduler.start()
  1. Using a blocking scheduler: The apscheduler library provides two types of schedulers, blocking and background. A blocking scheduler will run in the main thread and will not exit until the scheduler is stopped. This ensures that the application will stay alive as long as the scheduler is running.
from apscheduler.schedulers.blocking import BlockingScheduler

def my_job():
    print('Hello')

if __name__ == '__main__':
    scheduler = BlockingScheduler()
    scheduler.add_job(my_job, 'interval', seconds=10)
    scheduler.start()