Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Apscheduler - Schedule Jobs dynamically (nested)

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()
@sched.scheduled_job('cron', day_of_week='mon-fri', hour=6)
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__":
  sched.start()
  sched.shutdown(wait=True)

The queries are:

  • Is this the good way to add jobs dynamically?
  • The issue is, there could be 100 or more users. so, adding 100 jobs dynamically is a good idea?
  • Is there any other way to achieve this?
like image 301
Laxmikant Avatar asked Oct 18 '22 20:10

Laxmikant


1 Answers

APScheduler 3.0 was specifically designed to efficiently handle a high volume of scheduled jobs, so I believe your intended way of using it is valid.

like image 175
Alex Grönholm Avatar answered Oct 21 '22 17:10

Alex Grönholm