subprocess popen trouble

N

nik

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

################################################################
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
 
N

Nick Craig-Wood

nik said:
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!
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top