Python Tail: Tailing Log Files Without Blocking
In Python, tailing log files without blocking or locking can be achieved using various methods. One commonly used approach involves employing the subprocess module in combination with the select module.
Non-Blocking Tailing:
When working in a Linux environment, you can leverage the select module to poll the output pipe of a subprocess running a tail command. Here's an example:
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 uses the select.poll() method to check if there's new data available on the output pipe. When it detects new lines, it prints them.
Blocking Tailing:
For simpler implementation, you can use the subprocess module without the select module. However, this method blocks the script until the tail process is closed.
import subprocess f = subprocess.Popen(['tail', '-F', filename], stdout=subprocess.PIPE, stderr=subprocess.PIPE) while True: line = f.stdout.readline() print(line)
This code reads and prints new lines as they appear, but it blocks until the tail process is terminated.
The above is the detailed content of How Can I Tail Log Files in Python Without Blocking?. For more information, please follow other related articles on the PHP Chinese website!