Poco,子通信挂起(python 解释器是子)
Poco, child communication hangs (python interpreter is child)
我想与 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 & flush,谢谢@greatwolf
我的猜测是 python 的 print
函数没有直接输出到标准输出,或者幕后发生了一些有趣的事情。尝试用 sys.stdout.write
代替它。别忘了先import sys
我想与 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 & flush,谢谢@greatwolf
我的猜测是 python 的 print
函数没有直接输出到标准输出,或者幕后发生了一些有趣的事情。尝试用 sys.stdout.write
代替它。别忘了先import sys