473,378 Members | 1,404 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

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

Jul 22 '05 #1
5 1613
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

Jul 22 '05 #2
In article <11**********************@g43g2000cwa.googlegroups .com>,
"Dr. Who" <go****@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, do**@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

Jul 22 '05 #3
"Dr. Who" <go****@spiceaid.com> wrote in message
news:11*********************@z14g2000cwz.googlegro ups.com...
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

You can use the sentinel form of iter():

for line in iter(stdout.readline, ''):
print 'LINE:', line,
George
Jul 22 '05 #4
Dr. Who wrote:
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.


Python supports two different but related iterators over
lines of a file. What you show here is the oldest way.
It reads up to the newline (or eof) and returns the line.

The newer way is

for line in stdout:
...

which is equivalent to

_iter = iter(stdout)
while 1:
try:
line = _iter.next()
except StopIteration:
break

...

The file.__iter__() is implemented by doing
a block read and internally breaking the block
into lines. This make the read a lot faster
because it does a single system call for the
block instead of a system call for every
character read. The downside is that the read
can block (err, a different use of "block")
waiting for enough data.

If you want to use the for idiom and have
the guaranteed "no more than a line at a time"
semantics, try this

for line in iter(stdout.readline, ""):
print "LINE:", line
sys.stdout.flush()

Andrew
da***@dalkescientific.com

Jul 23 '05 #5
Donn Cave wrote:
In article <11**********************@g43g2000cwa.googlegroups .com>,
"Dr. Who" <go****@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, do**@u.washington.edu


Yes Donn's right , works the same for me , bufcallee.py may be should
look like
this

import time
import sys

sysout=sys.stdout

sysout.write("START\n")
sysout.flush()
time.sleep(10)
sysout.write("STOP\n")
sysout.flush()

regards
jitu

Jul 26 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Roy Smith | last post by:
I'm working on a prototype of a new application in Python. At some point, if this ever turns into a product, the powers that be will almost certainly demand that it be done in Perl. My job will...
42
by: Fred Ma | last post by:
Hello, This is not a troll posting, and I've refrained from asking because I've seen similar threads get all nitter-nattery. But I really want to make a decision on how best to invest my time....
17
by: Michael McGarry | last post by:
Hi, I am just starting to use Python. Does Python have all the regular expression features of Perl? Is Python missing any features available in Perl? Thanks, Michael
31
by: surfunbear | last post by:
I've read some posts on Perl versus Python and studied a bit of my Python book. I'm a software engineer, familiar with C++ objected oriented development, but have been using Perl because it is...
41
by: Xah Lee | last post by:
here's another interesting algorithmic exercise, again from part of a larger program in the previous series. Here's the original Perl documentation: =pod merge($pairings) takes a list of...
9
by: Dieter Vanderelst | last post by:
Dear all, I'm currently comparing Python versus Perl to use in a project that involved a lot of text processing. I'm trying to determine what the most efficient language would be for our...
20
by: Xah Lee | last post by:
Sort a List Xah Lee, 200510 In this page, we show how to sort a list in Python & Perl and also discuss some math of sort. To sort a list in Python, use the “sort” method. For example: ...
44
by: Tolga | last post by:
As far as I know, Perl is known as "there are many ways to do something" and Python is known as "there is only one way". Could you please explain this? How is this possible and is it *really* a...
13
by: squash | last post by:
I am a little annoyed at why such a simple program in Perl is causing so much difficulty for python, i.e: $a += 200000 * 140000; print $a;
8
by: Palindrom | last post by:
Hi everyone ! I'd like to apologize in advance for my bad english, it's not my mother tongue... My girlfriend (who is a newbie in Python, but knows Perl quite well) asked me this morning why...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.