how to flush child_stdin

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • joelcarrier@gmail.com

    how to flush child_stdin

    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("comman d 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?
  • joelcarrier@gmail.com

    #2
    Re: how to flush child_stdin

    On Feb 22, 12:15 am, Dennis Lee Bieber <wlfr...@ix.net com.comwrote:
    On Thu, 21 Feb 2008 12:34:28 -0800 (PST), "joelcarr...@gm ail.com"
    <joelcarr...@gm ail.comdeclaime d the following in comp.lang.pytho n:
    >
    >
    >
    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("comman d 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("comman d 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.netc om.com wulfr...@bestia ria.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.

    Comment

    • joelcarrier@gmail.com

      #3
      Re: how to flush child_stdin

      On Feb 22, 2:01 pm, Dennis Lee Bieber <wlfr...@ix.net com.comwrote:
      On Fri, 22 Feb 2008 08:35:03 -0800 (PST), "joelcarr...@gm ail.com"
      <joelcarr...@gm ail.comdeclaime d the following in comp.lang.pytho n:
      >
      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.netc om.com wulfr...@bestia ria.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.

      Comment

      • Gabriel Genellina

        #4
        Re: how to flush child_stdin

        En Fri, 22 Feb 2008 17:53:55 -0200, joelcarrier@gma il.com
        <joelcarrier@gm ail.comescribió :
        On Feb 22, 2:01 pm, Dennis Lee Bieber <wlfr...@ix.net com.comwrote:
        >On Fri, 22 Feb 2008 08:35:03 -0800 (PST), "joelcarr...@gm ail.com"
        ><joelcarr...@g mail.comdeclaim ed the following in comp.lang.pytho n:
        >>
        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('comman d 1\n')
        w.flush()
        print r.readline()
        w.write('comman d 2\n')
        w.flush()
        print r.readline()
        w.write('\n')
        w.flush()
        print r.readline()

        [slave.py]

        while True:
        line = raw_input().str ip()
        if not line:
        print "bye!"
        break
        print "echo:",lin e

        That works OK for me on Windows XP.

        --
        Gabriel Genellina

        Comment

        Working...