[Python-3000] iostack, second revision

Josiah Carlson jcarlson at uci.edu
Tue Sep 12 18:05:50 CEST 2006


"Anders J. Munch" <ajm at flonidan.dk> wrote:
> 
> Greg Ewing wrote:
> > Anders J. Munch wrote:
> > > any file that supports seeking to the end will also support
> > > reporting the file size.  Thus
> > >   f.seek(f.length)
> > > should suffice,
> > 
> > Although the micro-optimisation circuit in my
> > brain complains that it will take 2 system
> > calls when it could be done with 1...
> 
> I don't expect file methods and systems calls to map one to one, but
> you're right, the first time the length is needed, that's an extra
> system call.

Every time the length is needed, a system call is required (you can have
multiple writers of the same file)...

>>> import os
>>> a = open('test.txt', 'a')
>>> b = open('test.txt', 'a')
>>> a.write('hello')
>>> b.write('whee!!')
>>> a.flush()
>>> os.fstat(a.fileno()).st_size
5L
>>> b.flush()
>>> os.fstat(b.fileno()).st_size
11L
>>>


> My micro-optimisation circuitry blew a fuse when I discovered that
> seek always implies flush.  You won't get good performance out of code
> that does a lot of seeks, whatever you do.  Use my upcoming FileBytes
> class :)

Flushing during seek is important.  By not flushing during seek in your
FileBytes object, you are unnecessarily delaying writes, which could
cause file corruption.

 - Josiah



More information about the Python-3000 mailing list