APScheduler not starting?

╄→尐↘猪︶ㄣ 提交于 2019-12-01 02:51:00

you have to keep the script running otherwise after the sched.add_interval_job(myScript, start_date = startDate, days=1), the script ends and stop. add a

import time

while True:
    time.sleep(10)
sched.shutdown()

after, and then, the scheduler will still be alive.

The correct solution would be to tell the scheduler to not run as a daemon:

sched = Scheduler()
sched.daemonic = False

or

sched = Scheduler()
sched.configure({'apscheduler.daemonic': False})

here is my way:

from apscheduler.scheduler import Scheduler
def mainjob():
    print("It works!")

if __name__ == '__main__':
    sched = Scheduler()
    sched.start()
    sched.add_interval_job(mainjob,minutes=1)
    input("Press enter to exit.")
    sched.shutdown()

If you use version 2.1.0, you can also pass standalone=True parameter to the Scheduler constructor. Detail documents can be found here

from apscheduler.scheduler import Scheduler
from datetime import datetime, timedelta, time, date

def myScript():
    print "ok"

if __name__ == '__main__':
    sched = Scheduler(standalone=True)
    startDate = datetime.combine(date.today() + timedelta(days=1),time(1))
    sched.add_interval_job(myScript, start_date = startDate, days=1)
    sched.start()

I have apscheduler v3 installed and this is what I would do.

from apscheduler.schedulers.background import BackgroundScheduler  
def mainjob():
    print("It works!")

if __name__ == '__main__':
    sched = BackgroundScheduler()
    sched.start()
    sched.add_job(mainjob, 'interval', seconds=120)
    input("Press enter to exit.")
    sched.shutdown() 
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!