Edmund Lian wrote:
> The set up works well, except that console output does not seem to be being
> written to the log file in real time. Instead, it seems to be batch
> written.
Python's file I/O uses POSIX standard I/O. On Unix systems, standard
I/O buffers output to a terminal by lines, but output to files is
buffered by filesystem blocks. The standard I/O library does this to
make file output more efficient. See the setbuf(3) man page on a Unix
box near you.
You can observe this in a simple python script.
#!/usr/bin/python
import time
for i in range(1000):
for j in range(5):
print 'yo',
time.sleep(0.05)
print
Run that from a terminal. Note that you get a line of yo's at a time.
Run it redirected to a file, and note that you get nothing for a
while, then a whole bunch of yo's all at once.
$ python thatscript > outfile &
$ tail -f outfile
If you check the length of outfile while the script is running, you'll
see it's always an even number of blocks in size. (The block size is
4K on my Linux EXT3 filesystem, but different OSes or different
filesystems will vary. I've seen it as high as 64K and as low as
512 bytes.)
WebWare's console output is doing basically the same thing as the
little script.
I'm not sure what the best way to fix this is, but after groveling
through the Python source a little, it looks like this would work.
Add this code somewhere where it's executed early on.
import sys
sys.stdout.flush()
sys.stdout = file("mylogfile", "w", buffering=1)
# buffering=1 for line buffered, buffering=0 for unbuffered
In C, it's easy to change an open file's buffering midstream [sic]
but I don't see a way using the standard Python libraries.
--
Bob Miller K<bob>
kbobsoft software consulting
http://kbobsoft.com kbob@...
|