tail -f in python with no time.sleep
Question: I need to emulate "tail -f" in python, but I don't want to use time.sleep in
the reading loop. I want something more elegant like some kind of blocking
read, or select.select with timeout, but python 2.6 "select" documentation
specifically says: "it cannot be used on regular files to determine whether a
file has grown since it was last read." Any other way? In a few days if no
solution is given I will read tail's C source code to try to figure it out. I
hope they don't use sleep, hehe Thanks.

MarioR


Answer: (update) Either use FS monitors tools

  * For [linux](http://pyinotify.sourceforge.net/)
  * For [Windows](http://timgolden.me.uk/python/win32%5Fhow%5Fdo%5Fi/watch%5Fdirectory%5Ffor%5Fchanges.html)
  * For [Mac](http://pypi.python.org/pypi/pyobjc-framework-FSEvents/2.2b2)

Or a single sleep usage (which I would you consider as much more elegant).

    
    
    import time
    def follow(thefile):
        thefile.seek(0,2)      # Go to the end of the file
        while True:
             line = thefile.readline()
             if not line:
                 time.sleep(0.1)    # Sleep briefly
                 continue
             yield line
    
    logfile = open("access-log")
    loglines = follow(logfile)
    for line in loglines:
        print line
    

