zoukankan      html  css  js  c++  java
  • Python Sleep休眠函数

     1 #!/usr/bin/env python
     2 import os
     3 import time
     4 def fun(name):
     5     write_name="command %s failed!
    " % 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秒
  • 相关阅读:
    我的第一篇博客
    文献笔记5
    文献笔记4
    文献笔记8
    文献笔记6
    文献笔记10
    文献笔记7
    文献笔记1
    文献笔记2
    文献笔记3
  • 原文地址:https://www.cnblogs.com/qiernonstop/p/3663648.html
Copyright © 2011-2022 走看看