simple popen question

J

Jim Benson

Hi,

I use popen to run a binary executable.
A code snippit is below.
This part runs fine. I use readline()
to catch and print output from the binary
to the screen. The system obviously buffers
things. i.e. readline doesn't return after every
line of output from the executable.
Is there an easy way to turn off the buffering?
I tried adding the extra 'r' and a small bufsize
to the popen...no difference.

iFd = os.popen(self.sCmd + ' ' + sArgs)
# Run sCmd
while True:
line = iFd.readline()
if line == '':
# EOF found...break out.
break;

print '%s' % line[:-1]

iRet = iFd.close()

if (iRet != None):

print 'ERROR: error message'


Thanks,

Jim
 
C

Cameron Laird

Hi,

I use popen to run a binary executable.
A code snippit is below.
This part runs fine. I use readline()
to catch and print output from the binary
to the screen. The system obviously buffers
things. i.e. readline doesn't return after every
line of output from the executable.
Is there an easy way to turn off the buffering?
I tried adding the extra 'r' and a small bufsize
to the popen...no difference.

iFd = os.popen(self.sCmd + ' ' + sArgs)

# Run sCmd
while True:
line = iFd.readline()
if line == '':
# EOF found...break out.
break;

print '%s' % line[:-1]

iRet = iFd.close()

if (iRet != None):

print 'ERROR: error message'
.
.
.
In general, no; the reader doesn't control the buffering, the writer does.

But there are several tangential issues raised here. Most crucially: do
you control the (source of the) "binary executable"? If you do, what I
think you'll find most satisfying is to recompile it with buffering disabled,
at least on newlines.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,015
Latest member
AmbrosePal

Latest Threads

Top