[Tutor] Something that Perl can do that Python can't?

Smith, Jeff jsmith at medplus.com
Fri Jul 22 23:06:15 CEST 2005


Well, I finally managed to solve it myself by looking at some code.  The
solution in Python is a little non-intuitive but this is how to get it:

while 1:
    line = stdout.readline()
    if not line:
        break
    print 'LINE:', line,

If anyone can do it the more Pythonic way with some sort of iteration
over stdout, please let me know.

Jeff

-----Original Message-----
From: tutor-bounces at python.org [mailto:tutor-bounces at python.org] On
Behalf Of Smith, Jeff
Sent: Friday, July 22, 2005 4:37 PM
To: Tutor
Subject: [Tutor] Something that Perl can do that Python can't?


So here it is: handle unbuffered output from a child process.

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
_______________________________________________
Tutor maillist  -  Tutor at python.org
http://mail.python.org/mailman/listinfo/tutor


More information about the Tutor mailing list