[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

how to flush child_stdin

joelcarrier@gmail.com

2/21/2008 8:34:00 PM

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?
5 Answers

Dennis Lee Bieber

2/22/2008 5:15:00 AM

0

On Thu, 21 Feb 2008 12:34:28 -0800 (PST), "joelcarrier@gmail.com"
<joelcarrier@gmail.com> declaimed the following in comp.lang.python:

> 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
wlfraed@ix.netcom.com wulfraed@bestiaria.com
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: web-asst@bestiaria.com)
HTTP://www.bestiaria.com/

joelcarrier@gmail.com

2/22/2008 4:35:00 PM

0

On Feb 22, 12:15 am, Dennis Lee Bieber <wlfr...@ix.netcom.com> wrote:
> On Thu, 21 Feb 2008 12:34:28 -0800 (PST), "joelcarr...@gmail.com"
> <joelcarr...@gmail.com> declaimed the following in comp.lang.python:
>
>
>
> > 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
> wlfr...@ix.netcom.com wulfr...@bestiaria.com
> HTTP://wlfraed.home.netcom.com/
> (Bestiaria Support Staff: web-a...@bestiaria.com)
> HTTP://www.bestiaria.com/

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

Dennis Lee Bieber

2/22/2008 7:02:00 PM

0

On Fri, 22 Feb 2008 08:35:03 -0800 (PST), "joelcarrier@gmail.com"
<joelcarrier@gmail.com> declaimed the following in comp.lang.python:

> 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
wlfraed@ix.netcom.com wulfraed@bestiaria.com
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: web-asst@bestiaria.com)
HTTP://www.bestiaria.com/

joelcarrier@gmail.com

2/22/2008 7:54:00 PM

0

On Feb 22, 2:01 pm, Dennis Lee Bieber <wlfr...@ix.netcom.com> wrote:
> On Fri, 22 Feb 2008 08:35:03 -0800 (PST), "joelcarr...@gmail.com"
> <joelcarr...@gmail.com> declaimed the following in comp.lang.python:
>
> > 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
> wlfr...@ix.netcom.com wulfr...@bestiaria.com
> HTTP://wlfraed.home.netcom.com/
> (Bestiaria Support Staff: web-a...@bestiaria.com)
> 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.

Gabriel Genellina

2/26/2008 3:11:00 AM

0

En Fri, 22 Feb 2008 17:53:55 -0200, joelcarrier@gmail.com
<joelcarrier@gmail.com> escribió:
> On Feb 22, 2:01 pm, Dennis Lee Bieber <wlfr...@ix.netcom.com> wrote:
>> On Fri, 22 Feb 2008 08:35:03 -0800 (PST), "joelcarr...@gmail.com"
>> <joelcarr...@gmail.com> declaimed the following in comp.lang.python:
>>
>> > 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...
>
> 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.

--
Gabriel Genellina