Python Apscheduler - Schedule Jobs dynamically (nested)
Asked Answered
P

1

6

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?
Plane answered 11/12, 2015 at 8:8 Comment(2)
@AlexGrönholm - You would be able to answer my query, It looks like you are the author of the apscheduler and you have answered or commented most of the queries about apscheduler. apscheduler.readthedocs.org/en/latest/userguide.html Thanks in advance!Plane
I has a similar usecase where i need to run certain shell commands on daily basis using paramiko. I have a function run_shell. Can i replace it with user job and pass argument the entire command for eg. ls -lrt.Anima
M
4

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.

Marlenmarlena answered 12/12, 2015 at 1:1 Comment(3)
Thank you for quick response! I'm thinking of making this little more nested again. I will report the result by next week. Thanks again.Plane
My scheduler is not shutting down at all even though afer finishing the jobs. Here is the job list. Pl help. jobs [<Job (id=0479b0c6cc844879bcb555d262956890 name=APIManager.resume_user_serive)>]Plane
Did you expect the last line (sched.shutdown(wait=True)) to run after sched.start() or when did you expect it to run? Have you read the documentation for BlockingScheduler?Breadroot

© 2022 - 2025 — McMap. All rights reserved.