Something that Perl can do that Python can't?

Donn Cave donn at u.washington.edu
Fri Jul 22 17:09:59 EDT 2005


In article <1122064588.174698.153220 at g43g2000cwa.googlegroups.com>,
 "Dr. Who" <google at spiceaid.com> wrote:
> So here it is: handle unbuffered output from a child process.

Your Perl program works the same for me, on MacOS X,
as your Python program.  That's what we would expect,
of course, because the problem is with the (Python)
program on the other end - it's buffering output,
because the output device is not a terminal.

   Donn Cave, donn at u.washington.edu

> Here is the child process script (bufcallee.py):
> 	import time
> 	print 'START'
> 	time.sleep(10)
> 	print 'STOP'
> 
> In Perl, I do:
> 	open(FILE, "python bufcallee.py |");
> 	while ($line = <FILE>)
> 	{
> 	    print "LINE: $line";
> 	}
> 
> in which case I get
> 	LINE: START
> followed by a 10 second pause and then
> 	LINE: STOP
> 
> The equivalent in Python:
> import sys, os
> 
> FILE = os.popen('python bufcallee.py')
> for line in FILE:
>     print 'LINE:', line
> 
> yields a 10 second pause followed by
> 	LINE: START
> 	LINE: STOP
> 
> I have tried the subprocess module, the -u on both the original and
> called script, setting bufsize=0 explicitly but to no avail.  I also
> get the same behavior on Windows and Linux.
> 
> If anyone can disprove me or show me what I'm doing wrong, it would be
> appreciated.
> 
> Jeff



More information about the Python-list mailing list