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)
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
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!