Search code examples
pythonshutdownapscheduler

Python Apscheduler is not shutting down


We have a requirement to schedule multiple jobs dynamically while current job is executing.

Approximate Scenario is:

There should be a scheduler to go through a table of application daily basis (assume at 6 AM UTC). Find the users who has today's datetime as resume_dttime Dynamically schedule a job for that user and start his service at that today's resume_dttime So the my code is:

from apscheduler.schedulers.blocking import BlockingScheduler
sched = BlockingScheduler()

def scheduled_job():
    """
    """
    liveusers = todays_userslist() #Get users from table with todays resume_dttime
    for u in liveusers:
        user_job = get_userjob(u.id)
        runtime = u.resume_dttime #eg u.resume_dttime is datetime(2015, 12, 13, 16, 30, 5)
        sched.add_job(user_job, 'date', run_date=runtime, args=[u.name])


if __name__ == "__main__":
  scheduled_job()
  sched.start()
  sched.shutdown(wait=True)

Code works well (for one job). Not checked with multiple jobs, but it doesn't stop at all. I tried removing for loop. Also,have gone through the many queries on google. But no luck :( To Exit, every time, I need to exit through CTrl+C


Solution

  • It blocks at start() as the documentation says. The start() call will only return if the scheduler is shut down from another thread, which you're not doing anywhere (judging from the above snippet).