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秒

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM