views:

187

answers:

3

Hi!

Im sure there is a better way to do this, but I am quite the newbie so I did it the only way I could figure it out. The thing is, I have a script that updates a textfile with the newest posts from an RSS feed (I got some help from you guys to figure it out). But I want this script to be automated, so I made this:

import time
import os

seconds = 3600
kjor = 'python vg.py'

time.sleep(seconds)
os.system(kjor)

time.sleep(seconds)
os.system(kjor)

time.sleep(seconds)
os.system(kjor)

I continued with copying those 24x downwards. I know this problably can be done alot better with some loop (while?), but Im afraid I dont have alot of knowledge in that field (yet).

My question, however, is as following: Can the system be damaged in any way if I let this run over a longer period of time?

+1  A: 

Use xrange please, don't copying your code 24x times.

for loop in xrange(240):
    time.sleep(seconds)
    os.system(kjor)

It will not damage your system, as far as I know.

sunqiang
+8  A: 

To answer your question, no, this won't hurt anything. While the time.sleeps are sleeping, the program will take very little processing power and the rest of the system can run normally.

Now, as for your looping issue. If you want the code run forever (or until you stop the program) the code you want is

 while True:
    os.system(kjor)
    time.sleep(seconds)

This is, literally, and infinite loop, but in this case that (is probably) what you want.

If you are attached to having a particular number of iterations, then you could do something like sunqiang's answer (repeated here)

for loop in xrange(240):
    os.system(kjor)
    time.sleep(seconds)

Finally, if you are on a Unix platform (such as Linux or Mac) you should take a look at cron, as it is designed to set up recurring programs to run and particular time periods. You could set it up to run your script every minute and it will happily do so until the end of time (or you remove it, whichever comes first).

Mike Cooper
Windows has a similar functionality to cron(8), if thats what your lemons look like. There is a control panel called "Task Scheduler" (which you can search for in Explorer.exe).
TokenMacGuy
+1  A: 

It does not damage any system and it is pretty common as well.

Just create a loop so as your application will gently stop running after some time; Or better yet, make it check for a condition, maybe listen to a tcp port waiting for someone to ask it to quit (then you'll need to create a second application to send this quit message).

mamendex