On Tuesday 07 October 2008 05:12:28 pm Lawrence D'Oliveiro wrote: > In message <[EMAIL PROTECTED]>, Luis > > Zarrabeitia wrote: > > I have a problem with this piece of code: > > > > ==== > > import sys > > for line in sys.stdin: > > print "You said!", line > > ==== > > > > Namely, it seems that the stdin buffers the input, so there is no reply > > until a huge amount of text has bin written. The iterator returned by > > xreadlines has the same behavior. > > > > The stdin.readline() function doesn't share that behaviour (it returns as > > soon as I hit 'enter'). > > Perhaps line-buffering simply doesn't apply when you use a file object as > an iterator.
You cut out the question you replied to, but left the rest. I got a bit confused until I remembered that *I* wrote the email :D. Anyway, I changed the program to: === buff = file("test") for line in buff: print "you said", line === where 'test' is a named pipe (mkfifo test) to see if the line-buffering also happened with a file object, and it does. As with stdin, nothing gets printed until the end of the file or it receives a huge amount of lines, but using '.readline()' works immediately. So it seems that the buffering behavior happens by default on stdin and file. It makes sense, as type(stdin) is 'file'. I can't test it now, but I think the sockets also do input buffering. I guess one doesn't notice it on the general case because disk reading happens too fast to see the delay. That raises a related question: is there any use-case where is better to lock the input until a lot of data is received, even when the requested data is already available? Output buffering is understandable and desired (how do I turn it off, by the way?), and even that one wont lock unless requested to lock (flush), but I can't find examples where input buffering helps. (full example with pipes) $ mkfifo test $ cat > test [write data here] on another console, just execute the script. Oh, I forgot: Linux 2.6.24, python 2.5.2, Debian's standard build. I don't have windows at hand to try it. -- Luis Zarrabeitia (aka Kyrie) Fac. de Matemática y Computación, UH. http://profesores.matcom.uh.cu/~kyrie -- http://mail.python.org/mailman/listinfo/python-list