我希望与python & lua脚本进行通信,但是python不像预期的那样工作。Python挂起:
日志:
lua << 'hello!', lua >> 'hello!'
lua << 'hello!', lua >> 'hello!'
lua << 'hello!', lua >> 'hello!'
python << 'hello!', 主要C++应用程序:
#include <iostream>
#include "Poco/Process.h"
#include "Poco/PipeStream.h"
void test( char const* interpreter, char const* filename )
{
std::vector<std::string> args { filename };
Poco::Pipe outPipe;
Poco::Pipe inPipe;
Poco::ProcessHandle process_handle = Poco::Process::launch( interpreter, args, &inPipe, &outPipe , nullptr/*errPipe*/ );
Poco::PipeInputStream output_reader(outPipe);
Poco::PipeOutputStream input_writer(inPipe);
for(int repeat_counter=0; repeat_counter<3; ++repeat_counter)
{
auto send_str("hello!");
input_writer << send_str << std::endl;
std::cout << interpreter << " << '" << send_str << "', " );
std::cout.flush();
std::string receiv_str;
output_reader >> receiv_str;
std::cout << interpreter << " >> '" << receiv_str << "'" << std::endl;
}
}
int main()
{
test("lua","test.lua");
test("python","test.py");
return 0;
}Lua脚本:
for i=1,10 do
print(io.read())
endPython脚本:
for i in range(0,10):
print(raw_input())终端中的两个脚本的工作原理相同.
解决方案:因为Python必须只使用sys.stdout.write &刷新,谢谢@宏狼
发布于 2016-12-29 03:31:32
我猜python的print函数并不是直接输出到stdout,或者幕后发生了一些有趣的事情。试着用sys.stdout.write代替它。别忘了先去import sys。
https://stackoverflow.com/questions/41372633
复制相似问题