Python Sleep休眠函數

#!/usr/bin/env python

import os
import time

def fun(name):
    write_name="command %s failed!\n" % name
    print write_name
    f = open('/tmp/cs.log','a')
    f.write(write_name)
    f.close()

def tary(name):
    print "the command is %s" % name
    command_id = os.system(name)
    while command_id != 0:
        fun(name)
        time.sleep(10)
        command_id = os.system(name)

time.sleep(5)

tary("reboot")

 

Python 編程中使用 time 模塊可以讓程序休眠

 

具體方法是time.sleep(秒數),其中“秒數”以秒爲單位,可以是小數,0.1秒則代表休眠100毫秒。

# 例1:循環輸出休眠1秒
import time
i = 1
while i <= 3:
    print i # 輸出i
    i += 1
    time.sleep(1) # 休眠1秒

# 例1:循環輸出休眠100毫秒
import time
i = 1
while i <= 3:
    print i # 輸出i
    i += 1
    time.sleep(0.1) # 休眠0.1秒
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章