Home  >  Q&A  >  body text

Python 死循环处理不断输出的日志

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

迷茫迷茫2741 days ago546

reply all(2)I'll reply

  • 黄舟

    黄舟2017-04-18 10:28:30

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

    reply
    0
  • PHP中文网

    PHP中文网2017-04-18 10:28:30

    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.

    reply
    0
  • Cancelreply