Ask Your Question
0

What is the method to prevent apscheduler from causing the application to exit?

asked 2023-05-12 00:12:29 +0000

scrum gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
1

answered 2023-05-12 00:23:02 +0000

djk gravatar image

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()
edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-05-12 00:12:29 +0000

Seen: 11 times

Last updated: May 12 '23