subprocess popen trouble
Nick Craig-Wood
nick at craig-wood.com
Tue Jul 10 18:30:03 EDT 2007
nik <nikbaer at gmail.com> 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 <nick at craig-wood.com> -- http://www.craig-wood.com/nick
More information about the Python-list
mailing list