Python 死循环处理不断输出的日志
迷茫
迷茫 2017-04-18 10:26:30
0
2
575

对方接口的内容是在不断输出的(类似nginx访问日志) 我用Python写一个死循环去不停获取接口的内容, 但是内容会被重复输出, 请问大神,有什么方法对返回的内容进行处理, 每次循环只打印不同的内容?

迷茫
迷茫

业精于勤,荒于嬉;行成于思,毁于随。

reply all(2)
黄舟

If it is a log, it should have a timestamp. Using SQLite for recording and querying is an option

Ty80

There are 2 ways of thinking:

  • Use the file pointer seek function to store the position f.tell() after each read into a file, and start reading from the new position in the next loop

with open('access.log') as f:
    f.seek(n)
    ## processing 
    f.tell()
    ## save the newest postions to a file
  • A less relevant idea is that the system-level scheduled task cuts the nginx log at 0 o'clock every day and saves it by date. I have this script and it runs very stably. I can send it out when needed.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template