2
我想與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())
end
Python腳本:
for i in range(0,10):
print(raw_input())
在終端中,兩個腳本的工作原理完全相同。
解決方案:對於Python只能使用sys.stdout.write函數 & 沖洗,感謝@greatwolf
可能'raw_input()'等待'「\ n」'(Enter) – furas
@furas,std :: endl已經發送了「\ n」&flush() – biv
也許python的'print'做了一些有趣的事情場景,而不是直接輸出到標準輸出?怎麼樣嘗試'sys.stdout.write'? – greatwolf