我有一个C++可执行文件,在其中有以下代码行:
/* Do some calculations */
.
.
for (int i=0; i<someNumber; i++){
int inputData;
std::cin >> inputData;
std::cout<<"The data sent from Python is :: "<<inputData<<std::endl;
.
.
/* Do some more calculations with inputData */
}
而这个被循环调用的东西,我想在Python子进程中使用subprocess来调用这个可执行文件,就像这样:
p = Popen(['./executable'], shell=True, stdout=PIPE, stderr=PIPE, stdin=PIPE)
我可以通过可执行文件获取输出。
p.server.stdout.read()
但是我无法使用Python发送数据(整数)。
p.stdin.write(b'35')
由于 cin
在循环中被调用,因此 stdin.write
也应该被多次调用(在循环中)。上述情况是否可行?
有什么提示和建议可以帮助我实现它吗?谢谢。