Python Sleep休眠函数

 1 #!/usr/bin/env python
 2 import os
 3 import time
 4 def fun(name):
 5     write_name="command %s failed!\n" % name
 6     print write_name
 7     f = open('/tmp/cs.log','a')
 8     f.write(write_name)
 9     f.close()
10 def tary(name):
11     print "the command is %s" % name
12     command_id = os.system(name)
13     while command_id != 0:
14         fun(name)
15         time.sleep(10)
16         command_id = os.system(name)
17 time.sleep(5)
18 tary("reboot")

Python 编程中使用 time 模块可以让程序休眠

 

具体方法是time.sleep(秒数),其中“秒数”以秒为单位,可以是小数,0.1秒则代表休眠100毫秒。

 1 # 例1:循环输出休眠1秒
 2 import time
 3 i = 1
 4 while i <= 3:
 5     print i # 输出i
 6     i += 1
 7     time.sleep(1) # 休眠1秒
 8 
 9 # 例2:循环输出休眠100毫秒
10 import time
11 i = 1
12 while i <= 3:
13     print i # 输出i
14     i += 1
15     time.sleep(0.1) # 休眠0.1秒

 

你可能感兴趣的:(python)