APScheduler exits immediately after execution
Asked Answered
C

1

2

I am trying to contribute to a project on Github for collecting financial data.

The code..

    # time_keeper.py

    from apscheduler.scheduler import Scheduler  


    class TimeKeeper:
        def __init__(self):
            self.sched = Scheduler()

        def queue_job(self):
            print("message send to queue")

        def start_timers(self):
            self.sched.start()
            self.sched.add_cron_job(self.queue_job, minute='0-59')


    if __name__ == "__main__":
        from time_keeper import TimeKeeper

        TimeKeeper().start_timers()

The problem is, once the script is executed it runs for a split second then stops, there are no traceback errors.

Is the function incorrectly called or am I missing some parts of code? The Communities help would be much appreciated!

Consultation answered 8/2, 2017 at 17:21 Comment(4)
Maybe you're using an older version of APScheduler, but the import doesn't work for me of Scheduler. That said, there are multiple flavors of the scheduler. You likely want a BackgroundScheduler, unless your code is able to sit and block after you .start(). In that case, I'd probably start the scheduler in its own thread or process.Swirl
Thanks for your reply, I am using APScheduler 2.1.2.Consultation
@Swirl I just tried it with the newer version 3.x changed from apscheduler.scheduler import Scheduler to from apscheduler.schedulers.background import BackgroundScheduler and sched = Scheduler() to sched = BackgroundScheduler() I get the same result.Consultation
If you are using a BackgroundScheduler then you need to have the code do something to keep the main thread alive otherwise it will exit immediately. See github.com/agronholm/apscheduler/blob/master/examples/… and other examples in that directory of the repo.Swirl
S
1

The formal answer to your issue is that when using APScheduler v2, the default behavior of the scheduler is to run in threaded mode, which will return immediately after you apply the .start():

https://github.com/agronholm/apscheduler/blob/2.1/apscheduler/scheduler.py#L90-L91

Since it returns immediately and nothing keeps the main thread of your program alive, your program exits immediately. You need to keep your programming running long enough so that the scheduler can fire an event, or you need to run using a blocking version of the scheduler.

For this older version of APscheduler, you need to run in standalone mode if you want the scheduler to block:

https://github.com/agronholm/apscheduler/blob/2.1/examples/interval.py

or you if you want to continue running in threaded mode:

https://github.com/agronholm/apscheduler/blob/2.1/examples/threaded.py

Newer versions of APScheduler have separate BlockingScheduler andBackgroundScheduler` classes and you should consult the appropriate examples for the updated API.

Swirl answered 8/2, 2017 at 18:46 Comment(1)
Thank you for your detailed explanation, this has certainly put me on the right track.Consultation

© 2022 - 2025 — McMap. All rights reserved.