创建计时器计数器 - Python

时间:2014-09-25 03:51:54

标签: python mysql django

我正在使用Django进行项目,我有以下方法:

TIMEOUT = 10 # defines the timeout in sec
def read_cluster_log(proc, cluster):
    logs = ""
    while True:
       line = proc.stdout.readline()
       if not line:
          break
       logging.info(line)
       # here I need to add a buffer:
       # if the 'counter' is equal to 10 sec
       # append logs to the database and reset the timer to 0
       # else, keep appending.  
       logs += line
    cluster_log = ClusterLog(cluster=cluster, log=logs)
    cluster_log.save()
    return cluster_log

截至目前,此函数捕获整个输出,并将其保存到数据库中。 logBinaryField。我需要做的是设置一个从0到TIMEOUT的计时器,然后将当前日志追加到log的{​​{1}}字段。定时器重置为0,并一直持续到程序结束。 这里的想法是当用户创建cluster群集时,从网站提供输出。但是,这部分有点棘手。有人可以帮帮我吗? 运行此Hadoop应用程序的数据库是Django

1 个答案:

答案 0 :(得分:0)

尝试sleep(n),这会睡眠n秒。

import time

TIMEOUT = 10 # defines the timeout in sec
def read_cluster_log(proc, cluster):
    logs = ""
    while True:
       line = proc.stdout.readline()
       if not line:
          break
       logging.info(line)

       #changed here
       time.sleep(TIMEOUT) #sleeps for 10 seconds

       logs += line
    cluster_log = ClusterLog(cluster=cluster, log=logs)
    cluster_log.save()
    return cluster_log