subprocess popen trouble

Discussion in 'Python' started by nik, Jul 10, 2007.

  1. nik

    nik Guest

    I am having trouble using subprocess popen and stdin/stdout

    I have the following simple test application written in C++, that just
    echoes back the stdin. It works fine running from the command line:

    #include <iostream>
    #include <time.h>

    int main (int argc, char * const argv[]) {
    int currenttemp = 0;
    int settemp = 0;
    char* str;

    while(currenttemp < 500){
    gets(str);
    sscanf(str, ">%d", &settemp);
    currenttemp = settemp;
    printf("<%d\n", currenttemp++);
    }
    return 0;
    }

    ######################################################
    Then I have the following python code. A listener thread that waits
    for the output of test and the main part that creates the subprocess
    Popen object, passes it to the thread and then sends user input to the
    subprocess test.


    import subprocess
    import os, sys
    from threading import *
    import time

    class TempXListen(Thread):
    """ thread to listen for incomming
    messages and put them onto a
    shared queue, no time lost processing incoming lines
    """
    def __init__(self, channel):
    """ connect internal response queue
    to external queue
    """
    Thread.__init__(self)
    self.setDaemon(True)
    self._channel = channel
    self.start()

    def run(self):
    """ listening loop
    reads line off of input source
    puts the line into the response Q shared with parent
    thread
    sets event that parent is waiting for to indicate Q has at
    least 1 element
    """
    while 1:
    try:
    response = self._channel.stdout.readline() #
    read input line
    print response
    except Exception, e:
    print 'Listener Exception: ' + str(e)


    print "Temp Monitor Test"
    tempx = subprocess.Popen('/Users/engineeringadmin/Documents/test/build/
    Debug/test', \
    bufsize=1, stdin=subprocess.PIPE,
    stdout=subprocess.PIPE, \
    stderr=subprocess.PIPE)

    print tempx.stderr.readline()

    listener = TempXListen(tempx)
    while 1:
    data = sys.stdin.readline()
    if data != '':
    data = '>' + data + '\n'
    tempx.stdin.write(data)
    print data
    time.sleep(1)


    ###############################################################
    When I run it this is the output:

    Temp Monitor Test
    warning: this program uses gets(), which is unsafe.

    45
    >45


    ################################################################
    The subprocess is opened, because the warning is coming off of the
    test applications stderr, but nothing else seems to go in or out. I've
    been looking at a lot of examples and a lot of different postings, and
    can't see any mistakes in my use of subprocess. I would really
    appreciate it if somebody could see where I am going wrong.

    Thank you,
    Nik
     
    nik, Jul 10, 2007
    #1
    1. Advertising

  2. nik <> wrote:
    > I am having trouble using subprocess popen and stdin/stdout
    >
    > I have the following simple test application written in C++, that just
    > echoes back the stdin. It works fine running from the command line:
    >
    > #include <iostream>
    > #include <time.h>
    >
    > int main (int argc, char * const argv[]) {
    > int currenttemp = 0;
    > int settemp = 0;
    > char* str;
    >
    > while(currenttemp < 500){
    > gets(str);
    > sscanf(str, ">%d", &settemp);
    > currenttemp = settemp;
    > printf("<%d\n", currenttemp++);
    > }
    > return 0;
    > }


    You are being caught out by stdio buffering.

    You can
    a) put a fflush(stdout); after the printf()
    b) change the buffering mode of stdin/stdout with setvbuf()
    c) use pexpect which will connect to your prog with pseudo-ttys

    I recommend c) - subprocess isn't really very good at interactive
    conversations between the main process and the subprocess - buffering
    will cause you problems. You may in this simple case get it to work
    with a) or b) though!

    --
    Nick Craig-Wood <> -- http://www.craig-wood.com/nick
     
    Nick Craig-Wood, Jul 10, 2007
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Michele Simionato

    subprocess.Popen

    Michele Simionato, Dec 12, 2004, in forum: Python
    Replies:
    5
    Views:
    17,674
  2. Ivo Woltring
    Replies:
    4
    Views:
    3,561
    Jeff Shannon
    Jan 27, 2005
  3. John Abel
    Replies:
    2
    Views:
    3,314
    John Abel
    May 12, 2005
  4. Fernando

    Trouble with subprocess.Popen()

    Fernando, Apr 4, 2009, in forum: Python
    Replies:
    1
    Views:
    267
    Dennis Lee Bieber
    Apr 4, 2009
  5. File.popen/IO.popen

    , May 20, 2006, in forum: Ruby
    Replies:
    1
    Views:
    239
    Robert Klemme
    May 20, 2006
Loading...

Share This Page