0

I have a python script where a certain job needs to be done at say 8 AM everyday. To do this what i was thinking was have a while loop to keep the program running all the time and inside the while loop use scheduler type package to specify a time where a specific subroutine needs to start. So if there are other routines which run at different times of the day this would work.

def job(t):
    print "I'm working...", t
    return

schedule.every().day.at("08:00").do(job,'It is 08:00')

Then let windows scheduler run this program and done. But I was wondering if this is terribly inefficient since the while loop is waste of cpu cycles and plus could freeze the computer as the program gets larger in future. Could you please advise if there is a more efficient way to schedule tasks which needs to executed down to the second at the same time not having to run a while loop?

Slartibartfast
  • 1,058
  • 4
  • 26
  • 60
  • 2
    Its probably best to have a batch file that runs the script to perform the task that is called by the windows task scheduler at the correct time that you want to run the task at. Rather than having an endless running while loop. If it needs to be more precise than that you can start it up a minute or so before and have it wait until the exact milisecond you want to start. – Sean Powell Aug 20 '21 at 12:59
  • Were you able to solve your issue? – Life is complex Aug 31 '21 at 21:55
  • not yet because i am facing some timing issue with the code. Please give me another couple of days to mark this question as answered. Thanks – Slartibartfast Sep 01 '21 at 00:09

2 Answers2

2

I noted that you have a hard time requirement for executing your script. Just set your Windows Scheduler to start the script a few minutes before 8am. Once the script starts it will start running your schedule code. When your task is done exit the script. This entire process will start again the next day.

and here is the correct way to use the Python module schedule

from time import sleep
import schedule


def schedule_actions():

  # Every Day task() is called at 08:00
  schedule.every().day.at('08:00').do(job, variable="It is 08:00")

  # Checks whether a scheduled task is pending to run or not
  while True:
    schedule.run_pending()

    # set the sleep time to fit your needs
    sleep(1)


def job(variable):
    print(f"I'm working...{variable}")
    return


schedule_actions()

Here are other answers of mine on this topic:

Life is complex
  • 15,374
  • 5
  • 29
  • 58
1

Why a while loop ? Why not just let your Windows Scheduler or on Linux cron job run your simple python script to do whatever, then stop ?

Maintenance tends to become a big problem over time, so try to keep things as lightweight as possible.

colindaven
  • 39
  • 5
  • Still i need my code to run only at the specific time down to the second. So the program must start and finish in 2 seconds. So thats why i thought while loop – Slartibartfast Aug 20 '21 at 17:28
  • @Slartibartfast you just can add `sleep 16` before start script at Linux cron task. That will allow to start task at `08:00:16`. If you waiting some event between 8:00 and 8:30, just start loop at `8:00` and break it after event happens. Here is no necessary to run loop 24/7 if you able to control execution time. Anyway scheduling architecture is infrastructure problem, not program language, and depends on where you want to execute task. Your code must be easily deployable at WIndows, Linux and other different Cloud serverless platforms. So scheduling must be provided by platform. – rzlvmp Aug 25 '21 at 02:09