如何确保我们从 boost::child 进程中读取所有行
How to ensure that we read all lines from boost::child process
我在文档页面上看到了以下代码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个问题:-
- 如果
c.running()
返回 false,我们只需退出循环。在这种情况下,上面的流is
可能仍然携带丢失的数据? - 读取 stdout 和 stderr 的最佳方法是什么,同时确保进程 exit(( 不会造成死锁 页面上有一个警告说:-
如果你在nm退出后尝试读取,管道将导致死锁
我希望同时捕获stdout
和stderr
,而不必担心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);
- boost::进程间消息队列引发错误
- 在进程中对同一管道进行读取和写入时C++管道出现问题
- 是否可以通过C++扩展强制多个python进程共享同一内存
- IPC使用多个管道和分支进程来运行Python程序
- 异常属于C++中的线程还是进程
- WMI检测进程创建事件-c++
- c++多进程编写一个唯一的文件
- 如何在C++中将函数发送到另一个进程
- 在Qt Creator中,如何在连接到正在运行的进程后查看控制台输出
- 更改"child's parent's style sheet"时如何将孩子的样式表重置为 Qt 默认样式表?
- 终止 QProcess 不会终止子进程
- 将返回值从 exe 传递到 bat,并将其传递给 C# 中的进程
- COM :是否可以查看是否存在对我的某个 COM 对象的进程外引用?我可以释放它吗?
- Windows 进程间同步类似事件?
- 在挂钩启动新线程时解除挂钩进程
- pclose() 不会给我进程退出代码
- 运行代码时,c++ 会终止进程
- 可以读入进程内存的最大块大小是多少?
- 枚举进程模块在有效句柄上返回无效句柄
- 如何确保我们从 boost::child 进程中读取所有行