Exercise 8.2 - Solution

# follow.py
import os
import time

def follow(filename):
    '''
    Generator that produces a sequence of lines being written at the end of a file.
    '''
    f = open(filename, 'r')
    f.seek(0,os.SEEK_END)
    while True:
        line = f.readline()
        if line == '':
            time.sleep(0.1)    # Sleep briefly to avoid busy wait
            continue
        yield line

# Example use
if __name__ == '__main__':
    for line in follow('Data/stocklog.csv'):
        row = line.split(',')
        name = row[0].strip('"')
        price = float(row[1])
        change = float(row[4])
        if change < 0:
            print '%10s %10.2f %10.2f' % (name, price, change)
Discussion

The time.sleep() function is being used here to avoid busy-waiting on the CPU (e.g., sitting in an infinite loop with 100% CPU use while waiting for new lines to arrive). By sleeping, the program simply pauses for a short period waiting for more data to get added. When it wakes up, it will read all of the lines of data that accumulated while sleeping. No data is lost.

[ Back ]