Home  >  Article  >  Backend Development  >  How to Tail Log Files in Python: Blocking vs. Non-Blocking?

How to Tail Log Files in Python: Blocking vs. Non-Blocking?

Susan Sarandon
Susan SarandonOriginal
2024-11-20 19:54:18970browse

How to Tail Log Files in Python: Blocking vs. Non-Blocking?

Tailing Log Files with Python

One often wants to tail a log file or otherwise passively watch it to view the new content added to it. This can be achieved in Python in a few ways.

Non-Blocking

For this approach, one can use the subprocess and select modules.

import time
import subprocess
import select

f = subprocess.Popen(['tail', '-F', filename],
                      stdout=subprocess.PIPE, stderr=subprocess.PIPE)
p = select.poll()
p.register(f.stdout)

while True:
    if p.poll(1):
        print(f.stdout.readline())
    time.sleep(1)

This code opens a subprocess running tail -F on the specified file and polls its output for new data, printing it when available. This approach doesn't block the main program.

Blocking

A simpler blocking approach is available using only the subprocess module.

import subprocess

f = subprocess.Popen(['tail', '-F', filename],
                      stdout=subprocess.PIPE, stderr=subprocess.PIPE)
while True:
    line = f.stdout.readline()
    print(line)

This code also prints new lines as they are added, but it will block until the tail program is closed.

The above is the detailed content of How to Tail Log Files in Python: Blocking vs. Non-Blocking?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn