【发布时间】:2016-11-09 19:41:45
【问题描述】:
我正在使用 boost.process 启动另一个进程。
我想捕获标准输出并自己打印。
问题是,输出仅以块的形式打印或在停止子进程时打印。
测试子进程是一个python脚本,每秒调用echo "test" 20次。
void ExternalAppLauncher::setup()
{
boost::process::pipe stdout_p = boost::process::create_pipe();
boost::process::pipe stderr_p = boost::process::create_pipe();
{
file_descriptor_sink stdout_sink(stdout_p.sink, close_handle);
file_descriptor_sink stderr_sink(stderr_p.sink, close_handle);
file_descriptor_source stdout_source(stdout_p.source, close_handle);
file_descriptor_source stderr_source(stderr_p.source, close_handle);
out_stream.open(stdout_source);
err_stream.open(stderr_source);
childProcess.reset(new child(execute(
set_args(args),
bind_stdout(stdout_sink),
bind_stderr(stderr_sink),
inherit_env(),
// Guarantees that the child process gets killed, even when this process recieves SIGKILL(9) instead of SIGINT(2)
on_exec_setup([](executor&)
{
::prctl(PR_SET_PDEATHSIG, SIGKILL);
})
)));
}
}
// runs in another thread
void ExternalAppLauncher::run()
{
std::string s;
while (std::getline(err_stream, s))
{
std::cout << s;
}
}
这仅每 10 秒打印一次输出,可能是因为缓冲区需要在转发之前已满?
当我不调用bind_stdout() 时,输出会立即出现在控制台上。
有什么办法可以解决这个问题?
谢谢!
【问题讨论】: