使用 stdio 和 C++/Python 管道处理 EOF 问题
我在 python 进程和 C++ 程序之间的通信管道中遇到了 EOF 和 stdio 的一些问题。我不知道我做错了什么。当我在程序中看到 EOF 时,我清除标准输入,下一轮我尝试读取新行。问题是:由于某种原因, getline 函数立即返回一个 EOF 而不是等待来自 python 进程的新输入...知道吗?
好吧,这是代码:
#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;
}
在 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"
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. The 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"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
python中的
communicate
是一个一次性函数。它将给定的输入发送到进程,关闭输入流,并读取输出流,等待进程终止。在“通信”后,您无法使用相同的进程“重新启动”管道。
相反,在管道的另一侧,当您读取
EOF
时,不再有数据可读取。任何读取尝试都会立即返回EOF
; python 已关闭管道。如果您想继续与同一管道通信,则需要使用子进程的
stdin
和stdout
成员,而不是communicate
(但要小心死锁的可能性)并使用流结束以外的其他内容来表明 C++ 端应该进行另一“批”处理。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 returnEOF
; python has closed the pipe.If you want to carry on communicating with the same pipe you need to use the subprocess'
stdin
andstdout
members and notcommunicate
(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.