FAQ
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?

Search Discussions

  • Joelcarrier at Feb 22, 2008 at 4:35 pm

    On Feb 22, 12:15 am, Dennis Lee Bieber wrote:
    On Thu, 21 Feb 2008 12:34:28 -0800 (PST), "joelcarr... at gmail.com"
    <joelcarr... at 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... at ix.netcom.com wulfr... at bestiaria.com
    HTTP://wlfraed.home.netcom.com/
    (Bestiaria Support Staff: web-a... at bestiaria.com)
    HTTP://www.bestiaria.com/
    I don't think that is the problem, I'm feeding it newline characters.
  • Joelcarrier at Feb 22, 2008 at 7:53 pm

    On Feb 22, 2:01 pm, Dennis Lee Bieber 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...
    --
    Wulfraed Dennis Lee Bieber KD6MOG
    wlfr... at ix.netcom.com wulfr... at bestiaria.com
    HTTP://wlfraed.home.netcom.com/
    (Bestiaria Support Staff: web-a... at 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 at Feb 26, 2008 at 3:11 am
    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 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

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedFeb 21, '08 at 8:34p
activeFeb 26, '08 at 3:11a
posts4
users2
websitepython.org

2 users in discussion

Joelcarrier: 3 posts Gabriel Genellina: 1 post

People

Translate

site design / logo © 2022 Grokbase