How can I make a time delay in Python?

我想知道如何在Python脚本中加入时间延迟。


import time
time.sleep(5)   # delays for 5 seconds. You can Also Use Float Value.

这里是另一个例子,其中每分钟大概运行一次:

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

You can use the sleep() function in the time module. It can take a float argument for sub second resolution.

from time import sleep
sleep(0.1) # Time in seconds.

Please read https://web.archive.org/web/20090207081238/http://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

链接地址: http://www.djcxy.com/p/6564.html

上一篇: Clojure Emacs etags

下一篇: 我如何在Python中延迟时间?