On Wed Jun 06, 2007 at 21:18:42 +1000, Lindsay Holmwood wrote:
>Hi all,
>I've got an perplexing problem with buffered writing to files from stdout.
>
>I have a small Python program that's simulating the writing of a mail 
>log by reading in a file and printing out each line with a random delay 
>in between.
>
>When I run the program it prints out each line to the shell with the 
>delay as expected, but when I redirect stdout to a file the output is 
>written in big chunks of 30+ lines.
>
>Running the program through strace shows that when outputing to the 
>shell it does a write() then a select(), but when stdout is redirected 
>it only does a select(), and then a write every 20 or so select()s.
>
>My shell is bash, and my distro is Ubuntu Feisty. I get identical 
>behaviour from the same program in Ruby.
>
>Is anyone able to give me suggestions on what's going on here?
>

Off the top of my head.....

Python uses stdio, e.g: fread, fwrite, fopen.

When writing to a file, it will be buffered (probably 4k or so), when
writing to the terminal is will be line buffered.

You can change this using setbuf, but I'm not sure of the python equiv, of
the top of my head.

You can get around it easily by using file.flush() after each line.

hth,

B
_______________________________________________
coders mailing list
coders@slug.org.au
http://lists.slug.org.au/listinfo/coders

Reply via email to