【问题标题】:End a sleep loop in Python在 Python 中结束睡眠循环
【发布时间】:2015-11-09 19:34:12
【问题描述】:

我编写了一个 python 脚本,其中包含一个睡眠循环来监视一个日志文件,等待一个新的行来处理:

file = open('logFile.log', 'r')
while 1:
    where = file.tell()
    line = file.readline()
    if not line:
        time.sleep(1)
        file.seek(where)
    else:
        print line, # already has newline

我想对此进行修改,以便在没有换行的情况下花费超过 1 小时,结束循环并继续脚本。到目前为止还没有成功。

【问题讨论】:

    标签: python loops


    【解决方案1】:

    只保留一个计数器:

    count = 0
    file = open('logFile.log', 'r')
    while 1:
        where = file.tell()
        line = file.readline()
        if not line:
            count = count + 1
            if count >= 3600:
              break
            time.sleep(1)
            file.seek(where)
        else:
            print line, # already has newline
            count = 0
    # if you get here an hour without any newlines has passed
    

    【讨论】:

    • 最好将条件count >= 3600if 循环移动到while 循环,即while count!=3600。对吗?
    • 为了清楚起见,它可能会更好。在任何情况下,我都会使用不等式 - while count < 3600:。另外 - 我刚刚在 print 语句之后添加了一个count = 0 来重置计数器。
    【解决方案2】:

    这个问题的一个重点是 1 小时内没有新行CONTINUOUSLY,因此确实有必要在打印新行后重置计时器。

    count = 0
    one_hour = 60 * 60
    file = open('logFile.log', 'r')
    while 1:
        where = file.tell()
        line = file.readline()
        if not line:
            count = count + 1
            if count >= one_hour:
              break
            time.sleep(1)
            file.seek(where)
        else:
            print line
            count = 0   # Here needs to reset the time couter!
    

    【讨论】:

      【解决方案3】:

      while 1(顺便说一句,实际上应该写为while True)更改为检查在循环中花费了多长时间的条件。

      类似:

      file = ...
      now = time.time()
      
      while time.time() - now < 1 * 60 * 60:
      

      【讨论】:

        【解决方案4】:

        试试这段代码。

        file = open('logFile.log', 'r')
        
        while True:
            timeWait = 60*60 # Change the Value to something less to check whether the code works properly or not.
            where = file.tell()
            line = file.readline()
            if not line:
                time.sleep(1)
                timeWait = timeWait - 1
                file.seek(where)
                if timeWait == 0:
                    break
            else:
                print line
        

        【讨论】:

          【解决方案5】:

          您可以更改 while 以检查 1 小时时隙并重置 startTime,如下所示:

          file = open('logFile.log', 'r')
          startTime = time.time()
          while (time.time()-startTime) >= 3600:
              where = file.tell()
              line = file.readline()
              if not line:
                  time.sleep(1)
                  file.seek(where)
              else:
                  startTime = time.time() #record the last time output was available
                  print line, # already has newline
          

          【讨论】:

            猜你喜欢
            • 2022-01-05
            • 2012-11-15
            • 1970-01-01
            • 1970-01-01
            • 2017-10-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多