views:

1124

answers:

2

I want to know how to put delay in a Python script.

+17  A: 
import time
time.sleep(5) # delays for 5 seconds

Here is another example where something is run once a minute:

import time 
while True:
    print "This prints once a minute."
    time.sleep(60)  # Delay for 1 minute (60 seconds)
Evan Fosmark
A: 

Please read http://www.faqts.com/knowledge_base/view.phtml/aid/2609/fid/378, which can help you further:

Try the sleep function in the time module.

import time
time.sleep(60)

And put this in a while loop and a statement will only execute on the minute... That allows you to run a statement at predefined intervals regardless of how long the command takes (as long as it takes less than a minute or 5 or 60 or whatever you set it to) For example, I wanted to run a ping once a minute. If I just time.sleep(60) or time.sleep(45) even, the ping will not always take the same amount of time. Here's the code :)

time.sleep(time.localtime(time.time())[5])

The [5] just pulls the seconds out of the time.localtime()'s return value.

The great thing about time.sleep is that it supports floating point numbers!

import time
time.sleep(0.1)

http://python.org/doc/current/lib/module-time.html

tehvan
Only providing a link is not helpful.
Geoffrey Chetwood
Isn't it? I prefer linking to spamming. At least if the link contains useful data.
tehvan
Personally, I see no problem with linking to a helpful article.
Evan Fosmark
For something so simple it would be cooler to post the code. The link is nice however. I guess it comes down to the user and his preferences on how he wants the question-asked to come to get his answer.
SD
Links tend to break, if you quote the contents (with attribution), the content will be around as long as Stackoverflow is..
dbr