How to run a python script on loop for a period of time?

Viewed 1355

I have a python script which takes 30 seconds to run and output the data, I require. I want the python script to continually re-run after the python script has finished for a period of 15 minutes. I have tried using Windows Task Scheduler, but it can not be configured to run programs every 30 seconds (minimum is 1 minute). I also want to the python script to re-run after the python script is finished, not in intervals of 30 seconds.

1 Answers

How about something like this:

nest your script in a while(True):. At the end of your script, put a time.sleep(15*60). So:

def main():
    # do your script 

if __name__=="__main__":
    while(True):
        main()
        time.sleep(15*60) # time in seconds, so 30 if you want 30

With this, just run your script with python script.py. Can also run it in the background if you'd prefer to free up the console.

Related