1

I have an application I want to run on a virtual machine to update a database with data returned from an API every 15 minutes.

In researching the issue, it seems that the traditional approach is to use a scheduling library, etc (i.e. run task x every hour).

Is there anything inherently wrong with just using a recurring while loop and a time.sleep(900) delay? In other words, will it crash/ eat up too much ram/etc.. This program would run basically indefinitely on a virtual machine.

while True:
    print('hello world')
    time.sleep(900)
UBHDNNX
  • 61
  • 1
  • 9
  • 1
    For recursion, the program will run eventually out of stack depth and/or accidentally keep objects alive if not somehow “springboarding” - which is exactly what timing libraries take care of. – user2864740 Jan 16 '19 at 22:24
  • 4
    What's the recursive part of your code? All you posted is sequential. – a_guest Jan 16 '19 at 22:27
  • You have to [catch all exceptions](https://stackoverflow.com/questions/47103712/python-3-try-except-all-with-error), otherwise your script will die on the first error until you restart it. Using a [cron](https://en.wikipedia.org/wiki/Cron) job would probably be easier. – Boris Verkhovskiy Jan 16 '19 at 22:44
  • 1
    I think OP means *recurring*, not recursive. – Andrew Sun Jan 17 '19 at 00:36
  • You're right-- I meant recurring. I also have an if statement before the loop starts that should catch all exceptions. – UBHDNNX Jan 17 '19 at 02:35

1 Answers1

0

When in doubt, refer to the zen of python. If it is readable, easy to understand and does the job correctly then you’re good.

Having said that have a look here and here to see similar questions on how time.sleep works. You may change your mind depending on how accurate you need the sleep cycle to be

koopmac
  • 936
  • 10
  • 27