subprocess popen trouble

Gal Diskin gal.diskin at gmail.com
Tue Jul 10 23:32:50 CEST 2007


Your code seems correct to me.

Just as a long shot - have you tried setting  bufsize=0 ?


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;
> }
>
> ######################################################
> 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
>
>   
-- Gal Diskin
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20070711/5c2bba0c/attachment.html>


More information about the Python-list mailing list