如何确保我们从 boost::child 进程中读取所有行

How to ensure that we read all lines from boost::child process

本文关键字:进程 child 读取 boost 何确保 确保 我们      更新时间:2023-10-16

我在文档页面上看到了以下代码boost::child他们解释了如何读取子进程的输出。 http://www.boost.org/doc/libs/1_64_0/doc/html/boost_process/tutorial.html 他们说在运行您的子进程后,我们可以通过此循环读取它:-

bp::ipstream is; //reading pipe-stream
bp::child c(bp::search_patk("nm"), file, bp::std_out > is);
//then later
while (c.running() && std::getline(is, line) && !line.empty())
data.push_back(line);

我这里有2个问题:-

  1. 如果c.running()返回 false,我们只需退出循环。在这种情况下,上面的流is可能仍然携带丢失的数据?
  2. 读取 stdout 和 stderr 的最佳方法是什么,同时确保进程 exit(( 不会造成死锁 页面上有一个警告说:-
    如果你在nm退出后尝试读取,管道将导致死锁

我希望同时捕获stdoutstderr,而不必担心nm已经退出或不退出。

我认为除非使用异步方法,否则没有正确的方法。

也许你可以简单地得到一个向量的未来,如果你真的需要它,可以逐行使用它string_views。

std::future<std::vector<char> > output, error;
boost::asio::io_service svc;
bp::child c(bp::search_path("nm"), file, bp::std_out > output, bp::std_err > error, svc);
svc.run();

要像在矢量顶部使用 istream 之前一样读取:

#include <boost/process.hpp>
#include <boost/iostreams/device/array.hpp>
#include <boost/iostreams/stream_buffer.hpp>
#include <iostream>
namespace bp = boost::process;
namespace bio = boost::iostreams;
std::string const file = "./a.out";
int main() {
std::future<std::vector<char> > output, error;
boost::asio::io_service svc;
bp::child c(bp::search_path("nm"), file, bp::std_out > output, bp::std_err > error, svc);
svc.run();
//then later
{
auto raw = output.get();
std::vector<std::string> data;
std::string line;
bio::stream_buffer<bio::array_source> sb(raw.data(), raw.size());
std::istream is(&sb);
while (std::getline(is, line) && !line.empty())
data.push_back(line);
std::cout << data.at(rand()%data.size()) << "n";
}
}

我遇到了同样的问题... 解决此问题的最佳方法是使用异步 I/O。

不幸的是,提升文档@http://www.boost.org/doc/libs/master/doc/html/boost_process/extend.html#boost_process.extend.async 是错误的... 它使一切看起来都很简单,但没有显示必须事先调整缓冲区的大小,并且掩盖了许多细节。

这是我的函数,它一次性发送或接收一个缓冲区(没有像问题/响应 0 这样的交互,我使用 stderr 进行错误检查,因为这是我的应用程序所需要的,但您可以通过调用"c.exit_code((;"来捕获应用程序的退出代码。

using tstring=basic_string<TCHAR>;
void Run(
const tstring& exeName;
const tstring& args,
const std::string& input,
std::string& output,
std::string& error
)
{
using namespace boost;
asio::io_service ios;
std::vector<char> vOut(128 << 10);
auto outBuffer{ asio::buffer(vOut) };
process::async_pipe pipeOut(ios);
std::function<void(const system::error_code & ec, std::size_t n)> onStdOut;
onStdOut = [&](const system::error_code & ec, size_t n)
{
output.reserve(output.size() + n);
output.insert(output.end(), vOut.begin(), vOut.begin() + n);
if (!ec)
{
asio::async_read(pipeOut, outBuffer, onStdOut);
}
};
std::vector<char> vErr(128 << 10);
auto errBuffer{ asio::buffer(vErr) };
process::async_pipe pipeErr(ios);
std::function<void(const system::error_code & ec, std::size_t n)> onStdErr;
onStdErr = [&](const system::error_code & ec, size_t n)
{
error.reserve(error.size() + n);
error.insert(error.end(), vErr.begin(), vErr.begin() + n);
if (!ec)
{
asio::async_read(pipeErr, errBuffer, onStdErr);
}
};
auto inBuffer{ asio::buffer(input) };
process::async_pipe pipeIn(ios);
process::child c(
exeName + _T(" ") + args, 
process::std_out > pipeOut, 
process::std_err > pipeErr, 
process::std_in < pipeIn
);

asio::async_write(pipeIn, inBuffer, 
[&](const system::error_code & ec, std::size_t n) 
{
pipeIn.async_close();
});
asio::async_read(pipeOut, outBuffer, onStdOut);
asio::async_read(pipeErr, errBuffer, onStdErr);
ios.run();
c.wait();
}

我执行的应用程序是一个解码器/编码器,我发送整个文件进行处理,并以这种方式同时接收结果。 没有文件大小限制。

重要

boost 1.64 中有一个错误修复,显然只影响 Windows。

在文件提升\进程\详细信息\窗口\async_pipe.hpp 中: 参考: https://github.com/klemens-morgenstern/boost-process/issues/90

第 79 行:

~async_pipe()
{
//fix
//if (_sink .native()  != ::boost::detail::winapi::INVALID_HANDLE_VALUE_)
//    ::boost::detail::winapi::CloseHandle(_sink.native());
//if (_source.native() != ::boost::detail::winapi::INVALID_HANDLE_VALUE_)
//    ::boost::detail::winapi::CloseHandle(_source.native());
boost::system::error_code ec;
close(ec);
//fix
}

另一种解决方案。

bp::child c(bp::search_path("nm"), file, bp::std_out > output, bp::std_err > error, svc);

此代码忽略 stdout/stderr 的顺序。如果订单有问题,你可以这样写:

bp::child c(bp::search_path("nm"), file, (bp::std_out & bp::std_err) > outerr, svc);