Just had a quick look at the documentation for open:
http://docs.python.org/lib/built-in-funcs.html
I think that: sys.stdout = open("yourfile","w", 0)
will give you no buffer.

On 06/06/07, André Pang <[EMAIL PROTECTED]> wrote:

On 06/06/2007, at 9:18 PM, Lindsay Holmwood wrote:

> 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.
> [..]
> The Python source:
>
> #!/usr/bin/env python
> #
> # logfeeder.py
> # reads a specified file and outputs it in delayed chunks
> #
>
> import sys
> import random
> import time
>
> if sys.argv < 1:
>   print 'Usage logfeeder.py <input>'
>
> lines = file(sys.argv[1]).readlines()
> for line in lines:
>   print line.strip()
>   time.sleep(random.random() * 5)

I'm no Python nor UNIX tty expert, but try adding a flush() after
your print().  Terminal I/O (i.e. console stdout) is typically line-
buffered; file I/O is typically block-buffered.  Your "big chunks of
30+" lines might correspond very nicely to 1024/2048/4096 bytes.  See
also the setvbuf(3) manpage, setlinebuf(3) and friends... they
control line buffering on FILE* handles in C.  No idea what the
equivalent in Python is, but I'm sure there is one.

Hope that helps!


--
% Andre Pang : trust.in.love.to.save  <http://www.algorithm.com.au/>



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




--
Michael Connors
_______________________________________________
coders mailing list
coders@slug.org.au
http://lists.slug.org.au/listinfo/coders

Reply via email to