Piping EOF problems with stdio and C++/Python
I got some problems with EOF and stdio in a communication pipeline between a python process and a C++ program. I have no idea what I am doing wrong. When I see an EOF in my program I clear the stdin and next round I try to read in a new line. T开发者_运维问答he problem is: for some reason the getline function immediatly (from the second run always, the first works just as intended) returns an EOF instead of waiting for a new input from the python process... Any idea?
alright Here is the code:
#include <string>
#include <iostream>
#include <iomanip>
#include <limits>
using namespace std;
int main(int argc, char **argv) {
for (;;) {
string buf;
if (getline(cin,buf)) {
if (buf=="q") break;
/*****///do some stuff with input //my actual filter program
cout<<buf;
/*****/
} else {
if ((cin.rdstate() & istream::eofbit)!=0)cout<<"eofbit"<<endl;
if ((cin.rdstate() & istream::failbit)!=0)cout<<"failbit"<<endl;
if ((cin.rdstate() & istream::badbit)!=0)cout<<"badbit"<<endl;
if ((cin.rdstate() & istream::goodbit)!=0)cout<<"goodbit"<<endl;
cin.clear();
cin.ignore(numeric_limits<streamsize>::max());
//break;//I am not using break, because I
//want more input when the parent
//process puts data into stdin;
}
}
return 0;
}
and in python:
from subprocess import Popen, PIPE
import os
from time import sleep
proc=Popen(os.getcwd()+"/Pipingtest",stdout=PIPE,stdin=PIPE,stderr=PIPE);
while(1):
sleep(0.5)
print proc.communicate("1 1 1")
print "running"
communicate
in python is a one shot function. It sends the given input to a process, closes the input stream, and reads the output streams, waiting for the process to terminate.
There is no way you can 'restart' the pipe with the same process after "communicating".
Conversely, on the other side of the pipe, when you read EOF
there is no more data to read. Any attempt to read will immediately return EOF
; python has closed the pipe.
If you want to carry on communicating with the same pipe you need to use the subprocess' stdin
and stdout
members and not communicate
(but be careful of the potential of deadlocks) and use something other than the end of stream to signal that the C++ side should do another "batch" of processing.
精彩评论