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秒

 

转载于:https://www.cnblogs.com/qiernonstop/p/3663648.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值