On Wed, 8 Oct 2008 09:28:55 -0400, Luis Zarrabeitia <[EMAIL PROTECTED]> wrote:
On Tuesday 07 October 2008 11:27:19 pm George Sakkis wrote:
"""
In order to make a for loop the most efficient way of looping over the
lines of a file (a very common operation), the next() method uses a
hidden read-ahead buffer. As a consequence of using a read-ahead
buffer, combining next() with other file methods (like readline())
does not work right.
"""

I guess the phrasing "hidden read-ahead buffer" implies that buffering
cannot be turned off (or at least it is not intended to even if it's
somehow possible).

Hmm. I wonder how those optimizations look like. Apparently, readline() cannot
read from that read-ahead buffer, and that by itself sounds bad. Currently,
if you loop a few times with next, you cannot use readline afterwards until
you seek() to an absolute position.

Actually, I think I may be replying to myself here. I imagine that 'next' will
read a block instead of a character, and look for lines in there, and as the
underlying OS likely blocks until the whole block is read, 'next' cannot
avoid it. That doesn't explain, though, why readline() can't use next's
buffer, why next doesn't have a sensible timeout for interactive sessions
(unless the OS doesn't support it), and why the readahead cannot be turned
off.

I think I'll have to stick for now with the iter(function,sentinel) solution.

And I may try to find next()'s implementation... I guess I'll be downloading
python's source when my bandwidth allows it (or find it on a browseable
repository)

On a related note, help(file.read) shows:

=====
read(...)
   read([size]) -> read at most size bytes, returned as a string.

   If the size argument is negative or omitted, read until EOF is reached.
   Notice that when in non-blocking mode, less data than what was requested
   may be returned, even if no size parameter was given.
=====

But it doesn't say how to put the file object in non-blocking mode. (I was
trying to put the file object in non-blocking mode to test next()'s
behavior). ??Ideas?


Take a look at 
http://twistedmatrix.com/trac/browser/trunk/twisted/internet/fdesc.py

Jean-Paul
--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to