zoukankan      html  css  js  c++  java
  • python 实时遍历日志文件

    推荐日志处理项目:https://github.com/olajowon/loggrove

    首先尝试使用 python open 遍历一个大日志文件,

    使用 readlines() 还是 readline() ?

    总体上 readlines() 不慢于python 一次次调用 readline(),因为前者的循环在C语言层面,而使用readline() 的循环是在Python语言层面。

    但是 readlines() 会一次性把全部数据读到内存中,内存占用率会过高,readline() 每次只读一行,对于读取 大文件, 需要做出取舍。

    如果不需要使用 seek() 定位偏移, for line in open('file') 速度更佳。

    使用 readlines(),适合量级较小的日志文件

    复制代码
     1 p = 0
     2 with open(filepath, 'r+') as f:
     3     f.seek(p, 0)
     4     while True:
     5         lines = f.readlines()
     6         if lines:
     7             print lines
     8             p = f.tell()
     9             f.seek(p, 0)
    10         time.sleep(1)
    复制代码

    使用 readline(),避免内存占用率过大

    1 p = 0
    2 with open('logs.txt', 'r+') as f:
    3     while True:
    4         line = f.readline()
    5         if line:
    6             print line

    ################## 华丽分割 ##########################

    现在尝试使用 tail -F log.txt 动态输出

    由于 os.system() , commands.getstatusoutput() 属于一次性执行就拜拜, 最终选择 subprocess.Popen(),

    subprocess 模块目的是启动一个新的进程并与之通信,最常用是定义类Popen,使用Popen可以创建进程,并与进程进行交互。

    复制代码
    1 import subprocess
    2 import time
    3 
    4 p = subprocess.Popen('tail -F log.txt', shell=True, stdout=subprocess.PIPE,stderr=subprocess.PIPE,)
    5 while True:
    6    line = p.stdout.readline()
    7    if line:
    8         print line
    复制代码
  • 相关阅读:
    1028 人口普查 (20分)
    1027 打印沙漏 (20分)
    1026 程序运行时间 (15分)
    1025 反转链表 (25分)
    1024 科学计数法 (20分)
    1023 组个最小数 (20分)
    1022 D进制的A+B (20分)
    1021 个位数统计 (15分)
    1020 月饼 (25分)
    1019 数字黑洞 (20分)
  • 原文地址:https://www.cnblogs.com/sea520/p/10807509.html
Copyright © 2011-2022 走看看