read() does not read new content on FreeBSD/OpenBSD/OSX

Dieter tshinbum at gmail.com
Thu Jun 16 02:59:13 EDT 2011


Hi group,

I have a problem while reading from a file on BSD-like systems.
I have a writer process which continuously appends data to a file
and a reader (a data logger, something like tail -f), which should
read and analyse date from the file.
It works on Linux, but on BSD-like systems, it only reads one time,
then nothing more, although fstat shows that the file is growing.
And there's also a diff between tell() and the filelen.

If I write a reader in C, then it works, so the problem seems to be
in the python layer.

The writer:

while [ 1 ]; do
    echo `date` >> ./file
    sleep 1
done

The reader:

import time
import os

f=open("./file", "r")
while True:
    print "filelen %d, tell %d, read: %d" % (
      os.fstat(f.fileno()).st_size,
      f.tell(),
      len(f.read()))
    time.sleep(1.0)


On Linux:
dieter at linuxbox$ python reader.py 
filelen 15215, tell 0, read: 15215
filelen 15215, tell 15215, read: 0
filelen 15251, tell 15215, read: 36
filelen 15251, tell 15251, read: 0
filelen 15285, tell 15251, read: 34
filelen 15285, tell 15285, read: 0

On FreeBSD/OpenBSD/MacOS:
dieter at osx$ python reader.py 
filelen 183147, tell 0, read: 183147
filelen 183180, tell 183147, read: 33
filelen 183180, tell 183180, read: 0
filelen 183606, tell 183180, read: 0
filelen 183606, tell 183180, read: 0

I began to analyse it with strace/ktrace, but maybe I am missing something.
Are there any special switches/flags for BSD?

dieter



More information about the Python-list mailing list