how to flush child_stdin

J

joelcarrier

I'm opening up a subprocess like this where slave.py is a text based
app that receives commands and responds with output:

r, w, e = popen2.popen3('python slave.py')

I need to send slave.py a command and see the output,
so I'll do something like:

w.write("command here")
then i'll try this:
w.flush()

A separate thread is reading from r to retrieve output of slave.py.

The problem is that slave.py doesn't seem to receive commands unless I
also do:
w.close()

But I don't want to do this because I'll want to send more commands.

Any idea what is going on?
 
D

Dennis Lee Bieber

I'm opening up a subprocess like this where slave.py is a text based
app that receives commands and responds with output:

r, w, e = popen2.popen3('python slave.py')

I need to send slave.py a command and see the output,
so I'll do something like:

w.write("command here")
then i'll try this:
w.flush()

A separate thread is reading from r to retrieve output of slave.py.

The problem is that slave.py doesn't seem to receive commands unless I
also do:
w.close()
What happens if you do:

w.write("command here\n")
w.flush()

Could the slave be blocked waiting for an EOL character before
processing said command?
--
Wulfraed Dennis Lee Bieber KD6MOG
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 
D

Dennis Lee Bieber

I don't think that is the problem, I'm feeding it newline characters.

It wasn't shown in your sample, so I jumped on the first likely
thing...

The second is in the hands of the subprocess... While you are
flushing output /to/ the subprocess, is IT flushing its output (the
stuff you are trying to read). A common problem seems to be that, as
soon as the process detects a pipe, it goes to buffered I/O, and if the
buffer isn't filled, the parent has no access...
--
Wulfraed Dennis Lee Bieber KD6MOG
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 
J

joelcarrier

It wasn't shown in your sample, so I jumped on the first likely
thing...

The second is in the hands of the subprocess... While you are
flushing output /to/ the subprocess, is IT flushing its output (the
stuff you are trying to read). A common problem seems to be that, as
soon as the process detects a pipe, it goes to buffered I/O, and if the
buffer isn't filled, the parent has no access...
--
Wulfraed Dennis Lee Bieber KD6MOG
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/

I'm actually running something like : r, w, e = popen2.popen3('python -
u slave.py')
to try and force unbuffered. slave.py is basically outputting by
using print.
I guess it might still be buffering?
Anyway, thanks for your thoughts... I may have to take an entirely
difference approach. I was hoping not to have to touch the code base
represented by slave.py.
 
G

Gabriel Genellina

En Fri, 22 Feb 2008 17:53:55 -0200, (e-mail address removed)
I'm actually running something like : r, w, e = popen2.popen3('python -
u slave.py')

That was not on your posted example either...
to try and force unbuffered. slave.py is basically outputting by
using print.
I guess it might still be buffering?
Anyway, thanks for your thoughts... I may have to take an entirely
difference approach. I was hoping not to have to touch the code base
represented by slave.py.

[master.py]

import popen2
r, w, e = popen2.popen3('python -u slave.py')

w.write('command 1\n')
w.flush()
print r.readline()
w.write('command 2\n')
w.flush()
print r.readline()
w.write('\n')
w.flush()
print r.readline()

[slave.py]

while True:
line = raw_input().strip()
if not line:
print "bye!"
break
print "echo:",line

That works OK for me on Windows XP.
 

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,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top