Search code examples
pythonpython-3.xscheduling

time.sleep() or scheduling script. Which one is better approach?


I have a small script which I want to run after every 15 minutes. I can achieve it in two ways:

  1. By putting whole code in while loop while True: and at end time.sleep(900).

  2. By Scheduling a job to run the script after every 15 minutes once.

Both will work fine, but I am not sure whether the script keeps the resource busy while sleeping.

Please suggest.... Which one is better approach?


Solution

  • I think the scheduling system like cron on Linux (don't know about Windows, but I woud expect a similar situation there) is the way to go because of its numerous advantages:

    • you can rely on it, it is a robust and mature system
    • it starts a fresh process every time, thus saving resources and protecting the system from a possible memory or file descriptor leak in a long running program
    • it mails the output and reports crashes to the owner
    • you don't have to put your process to the background

    The disadvantages:

    • if you need to remember a state between runs, you have to save it to a file