首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >读取一个文件10秒钟

读取一个文件10秒钟
EN

Stack Overflow用户
提问于 2013-10-24 06:24:47
回答 2查看 227关注 0票数 0

我现在在和Logfiles一起工作。我的需要是,我想逐行读取一个指定的时间段,比如10s。如果有办法在Python中实现这一点,有人能帮我吗?

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2013-10-24 06:37:26

使用tailtac运行Popen并对输出进行迭代,直到找到要停止的行为止。下面是一个示例片段。

代码语言:javascript
复制
filename = '/var/log/nginx/access.log'
# Command to read file from the end
cmd = sys.platform == 'darwin' and ['tail', '-r', filename] or ['tac', filename]
# But if you want read it from beginning, use the following
#cmd = ['cat', filename]

proc = Popen(cmd, close_fds=True, stdout=PIPE, stderr=PIPE)
output = proc.stdout

FORMAT = [
    # 'foo',
    # 'bar',
]
def extract_log_data(line):
    '''Extact data in you log format, normalize it.
    '''
    return dict(zip(FORMAT, line))

csv.register_dialect('nginx', delimiter=' ', quoting=csv.QUOTE_MINIMAL)
lines = csv.reader(output, dialect='nginx')
started_at = dt.datetime.utcnow()
for line in lines:
    data = extract_log_data(line)
    print data
    if (dt.datetime.utcnow() - started_at) >= dt.timedelta(seconds=10):
        break

output.close()
proc.terminate()
票数 1
EN

Stack Overflow用户

发布于 2013-10-24 07:02:57

代码

代码语言:javascript
复制
from multiprocessing import Process
import time

def read_file(path):
    try:
        # open file for writing
        f = open(path, "r")
        try:
            for line in f:
                # do something
                pass

        # always close the file when leaving the try block 
        finally:
            f.close()

    except IOError:
        print "Failed to open/read from file '%s'" % (path)

def read_file_limited_time(path, max_seconds):

    # init Process
    p = Process(target=read_file, args=(path,))

    # start process
    p.start()

    # for max seconds 
    for i in range(max_seconds):

        # sleep for 1 seconds (you may change the sleep time to suit your needs)
        time.sleep(1)

        # if process is not alive, we can break the loop
        if not p.is_alive():
            break

    # if process is still alive after max_seconds, kiil it!
    if p.is_alive():
        p.terminate()

def main():
    path = "f1.txt"
    read_file_limited_time(path,10)

if __name__ == "__main__":
    main()

备注

我们每1秒“醒来”并检查我们开始的进程是否还活着的原因仅仅是为了阻止我们在进程结束时继续睡眠。如果过程在1秒后结束,则睡眠时间浪费9秒。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19558467

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档