Writing a tail -f python script that doesn't use 100% cpu
Here is a simple tail -f implementation written in python. The problem with this has to do with the nature of the loop, this script loves a lot of CPU time. If this is what you would like to run as a forked process / daemon, that would be an inefficient start.
What solution has an efficient tail -f CPU written in Python?
# CPU inefficient tail -f
def main():
filename = '/tmp/foo'
file = open(filename, 'r')
st_results = os.stat(filename)
st_size = st_results[6]
file.seek(st_size)
lastmatch = 0
while 1:
where = file.tell()
line = file.readline()
if not line:
time.sleep(1)
file.seek(where)
else:
print line,
source to share
I am not aware of an implementation that will be extremely efficient and portable between Windows on the one hand and virtually any other system on the other. Almost everywhere I would use a select
standard library module (which can be based on system-level functionality like select, kqueue, etc.) to wake up if and only if there are changes in the file, no polling; but on windows select can only work on sockets. This way, on Windows, I would use directory change notifications (e.g. via ctypes or win32).
source to share