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?
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)
The output from strace with no redirection:
fstat64(1, {st_mode=S_IFCHR|0620, st_rdev=makedev(136, 4), ...}) = 0
mmap2(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1,
0) = 0xb7f89000
write(1, "Jun 3 06:25:44 quasimodo postfi"..., 119Jun 3 06:25:44
quasimodo postfix/cleanup[24849]: C5961585AD:
message-id=<[EMAIL PROTECTED]> ) = 119
select(0, NULL, NULL, NULL, {2, 500595}) = 0 (Timeout)
write(1, "Jun 3 06:25:47 quasimodo postfi"..., 190Jun 3 06:25:47
quasimodo postfix/smtp[24850]: C5961585AD: to=<[EMAIL PROTECTED]>,
orig_to=<root>, relay=none, delay=4, status=bounced (mail for
mail.unstated.net loops back to myself) ) = 190
select(0, NULL, NULL, NULL, {4, 653147}) = 0 (Timeout)
write(1, "Jun 3 06:25:47 quasimodo postfi"..., 119Jun 3 06:25:47
quasimodo postfix/cleanup[24849]: 0607F58626:
message-id=<[EMAIL PROTECTED]>
) = 119
select(0, NULL, NULL, NULL, {2, 577691}) = 0 (Timeout)
write(1, "Jun 3 06:25:47 quasimodo postfi"..., 174Jun 3 06:25:47
quasimodo postfix/smtp[24850]: 0607F58626: to=<[EMAIL PROTECTED]>,
relay=none, delay=0, status=bounced (mail for mail.unstated.net loops
back to myself) ) = 174
select(0, NULL, NULL, NULL, {3, 826851} <unfinished ...>
The output from strace with redirection:
fstat64(1, {st_mode=S_IFREG|0644, st_size=0, ...}) = 0
mmap2(NULL, 4096, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1,
0) = 0xb7f91000
select(0, NULL, NULL, NULL, {0, 349926}) = 0 (Timeout)
select(0, NULL, NULL, NULL, {0, 201023}) = 0 (Timeout)
select(0, NULL, NULL, NULL, {4, 204536}) = 0 (Timeout)
select(0, NULL, NULL, NULL, {2, 18747}) = 0 (Timeout)
select(0, NULL, NULL, NULL, {1, 115257}) = 0 (Timeout)
select(0, NULL, NULL, NULL, {3, 145115}) = 0 (Timeout)
select(0, NULL, NULL, NULL, {0, 660363}) = 0 (Timeout)
select(0, NULL, NULL, NULL, {4, 108823}) = 0 (Timeout)
Cheers!
Lindsay
_______________________________________________
coders mailing list
coders@slug.org.au
http://lists.slug.org.au/listinfo/coders