2

I am trying to find the best way to schedule a function to execute in my python program. Currently I have the following code; which works but I am curious if there is a better way.

from datetime import datetime

start_time =  datetime(2019, 7, 26, 14, 48, 10, 28061)

def do_something():
    return 2+2

while True:
    if (datetime.now() > start_time):
        do_something()
        break

Is this a BAD way to do this? Is there a better way to do this? Thanks to all of those who reply in advance.

Kyle DeGennaro
  • 188
  • 3
  • 12
  • 1
    you should use cron jobs or scheduler libraries in python such as [sched](https://docs.python.org/3/library/sched.html) – amanb Jul 25 '19 at 19:09
  • I agreed with @amanb. It is more maintainable to use cron jobs or scheduler libraries since those will also take care of terminate/stop/start/restart/alert of the execution should unexpected result were to happen. With the proposed approach, you might not even know that the code stop running until something bad happens. – frostshoxx Jul 25 '19 at 20:08
  • Assuming you're on linux, "jobber" is another gem for scheduling jobs. – altunyurt Jul 31 '19 at 18:41

0 Answers0