【问题标题】:Python calculate lines per second / minute being written to a filePython计算每秒/分钟写入文件的行数
【发布时间】:2012-05-17 19:02:38
【问题描述】:

我有兴趣构建一个 python 脚本,它可以为我提供关于每个间隔(可能是一分钟)写入文件的行数的统计信息。我有在数据进入时正在写入的文件,每个用户都有一个新行,通过外部程序传递数据。知道每个 x 有多少行给了我一个可以用于未来扩展计划的指标。输出文件由行组成,所有行的长度都相对相同,并且最后都带有行返回。我正在考虑编写一个脚本,该脚本执行以下操作:在特定点测量文件的长度,然后在未来的另一个点再次测量它,减去两者并得到我的结果......但我不知道如果这是理想的,因为测量文件的长度需要时间,这可能会扭曲我的结果。有没有人有其他想法?

根据人们的说法,我把它放在一起开始:

import os
import subprocess
import time
from daemon import runner
#import daemon

inputfilename="/home/data/testdata.txt"

class App():
    def __init__(self):
        self.stdin_path = '/dev/null'
        self.stdout_path = '/dev/tty'
        self.stderr_path = '/dev/tty'
        self.pidfile_path =  '/tmp/count.pid'
        self.pidfile_timeout = 5
    def run(self):
        while True:
            count = 0

            FILEIN = open(inputfilename, 'rb')
            while 1:
              buffer = FILEIN.read(8192*1024)
              if not buffer: break
              count += buffer.count('\n')
            FILEIN.close(  )
            print count
            # set the sleep time for repeated action here:
            time.sleep(60)

app = App()
daemon_runner = runner.DaemonRunner(app)
daemon_runner.do_action()

它的工作是每 60 秒获取一次计数并将其打印到屏幕上,我的下一步是我猜的数学。

另一个编辑:我以一分钟的间隔添加了计数的输出:

import os
import subprocess
import time
from daemon import runner
#import daemon

inputfilename="/home/data/testdata.txt"


class App():
    def __init__(self):
        self.stdin_path = '/dev/null'
        self.stdout_path = '/dev/tty'
        self.stderr_path = '/dev/tty'
        self.pidfile_path =  '/tmp/twitter_counter.pid'
        self.pidfile_timeout = 5
    def run(self):
        counter1 = 0
        while True:
            count = 0

            FILEIN = open(inputfilename, 'rb')
            while 1:
              buffer = FILEIN.read(8192*1024)
              if not buffer: break
              count += buffer.count('\n')
            FILEIN.close(  )

            print count - counter1

            counter1 = count
            # set the sleep time for repeated action here:
            time.sleep(60)

app = App()
daemon_runner = runner.DaemonRunner(app)
daemon_runner.do_action()

【问题讨论】:

  • 线条的大小是否相同或非常相似?虽然计算一个非常大的文件中的行数需要很长时间,但查找文件的大小要快得多。
  • 您是否能够编辑正在写入数据的程序?你可以修改它,让它定期报告它写了多少行。
  • 线条有些相似,例如姓名、地址等...但是如果该人写得更多,则某些调查问题的答案会更长...但是我同意找到平均线条大小和文件大小和一些设计可能是要走的路......我没有能力修改输入程序,但我可以与编写它的人交谈并询问他们是否可以。
  • 无论您最终得到什么解决方案,我都建议您针对专门的软件(例如 wc)进行计时。

标签: python count line


【解决方案1】:

要评论您的想法(在我看来这听起来很不错),您需要测量的准确度如何?

我建议先测量测量时间。然后,给定您想要达到的相对精度,您可以计算连续测量之间的时间间隔,例如如果测量需要 t 毫秒并且您想要 1% 的准确度,请不要在 100t 内多次测量女士。

虽然测量时间会随着文件的增长而增长,但您必须牢记这一点。

关于如何计算文件行数的提示:is there a built-in python analog to unix 'wc' for sniffing a file?

关于如何测量时间的提示:time 模块。

附:我只是尝试在 245M 文件上计时行计数器。第一次大约需要 10 秒(第一次运行时没有计时),但它总是低于 1 秒。也许那里已经完成了一些缓存,我不确定。

【讨论】:

  • @ Lev Levitky:根据您的评论,我在脚本开头编辑了我的上述帖子
  • 轻松:我已经在上面发布了我的解决方案
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-23
  • 2016-09-27
  • 2023-02-16
  • 2018-12-16
相关资源
最近更新 更多