how to flush child_stdin

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Tue Feb 26 04:11:07 CET 2008


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




More information about the Python-list mailing list