Home > Backend Development > Python Tutorial > How to Tail Log Files in Python Without Blocking?

How to Tail Log Files in Python Without Blocking?

Patricia Arquette
Release: 2024-11-16 19:58:03
Original
192 people have browsed it

How to Tail Log Files in Python Without Blocking?

Tailing Log Files in Python

Q: Is there a non-blocking or locking way to tail a log file in Python, similar to the command tail -F? While there's an older method, is there a better solution or library available?

A: Non-Blocking:

On Linux, utilizing the subprocess and select modules offers a non-blocking solution:

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)
Copy after login

This solution polls the output pipe for new data and displays it immediately. You can replace time.sleep(1) and print f.stdout.readline() with custom functionalities.

Blocking:

For a blocking approach, you can use the subprocess module without additional modules:

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

This method will also print new lines as they emerge, but it will halt execution until the tail process is terminated (e.g., via f.kill()).

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

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template