faqts : Computers : Programming : Languages : Python : Modules : time

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

1218 of 1255 people (97%) answered Yes
Recently 10 of 10 people (100%) answered Yes

Entry

Does python have a sleep function? e.g. sleep(60) to wait 60 seconds?
Is there a command/function in python to put a delay or wait step into a program?

Jul 9th, 2000 23:28
unknown unknown, Nathan Wallace, Greg Fortune, pehr anderson


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