1

我尝试将boost:: childprocess 与 async_pipe 一起使用,如下面的代码示例所示,同时期望由于有一个等待方法,运行调用不会等待被调用的可执行文件完成,然后继续执行我所在的行调用wait()。我的目标是多次启动同一个可执行文件,以便在 GTest 中测试一个实例计数方法(基于 boost 托管共享内存段实现)。 但是在这里我需要调用io_service::run(),而不是像现在这样等待被调用的可执行文件完成。有人可以告诉我我在哪里使用错了吗?或者,如果这是对我的功能进行单元测试的错误方法?一段时间以来,我一直在努力寻找解决方案! 这是我如何调用可执行文件的一个实例的示例:

int CallChildProcess_Style9() {

std::string strCmdLine = "E:\\file.exe --Debug MainStartUps_Off --Lock 3";
boost::asio::io_service m_oIOS;
std::vector<char>       m_oAsyncBuffer_Out;
bp::async_pipe          m_oAsyncPipe_Out(m_oIOS);
std::error_code         build_ec;
size_t                  nReadSize(0);
boost::scoped_ptr<boost::process::child>  m_pChildProcess(nullptr);

m_pChildProcess.reset(new bp::child(strCmdLine.data(), bp::std_out > m_oAsyncPipe_Out, build_ec));

m_oAsyncBuffer_Out.resize(1024*8);

boost::asio::async_read(m_oAsyncPipe_Out, boost::asio::buffer(m_oAsyncBuffer_Out),
    [&](const boost::system::error_code &ec, std::size_t size) { nReadSize = size; });

size_t iii = m_oIOS.run();

m_pChildProcess->wait();
m_oAsyncBuffer_Out.resize(nReadSize);

std::string strBuf(m_oAsyncBuffer_Out.begin(), m_oAsyncBuffer_Out.begin() + nReadSize);

int         result = m_pChildProcess->exit_code();

m_oAsyncPipe_Out.close();

m_oIOS.reset();

return result;

}

4

1 回答 1

0

使用io_service

要使用async_pipe,您需要将io_service实例提供给 的参数关键字bp::child

#include <boost/asio.hpp>
#include <boost/process.hpp>
#include <boost/process/async.hpp>
#include <boost/scoped_ptr.hpp>
#include <iostream>

namespace bp = boost::process;

int CallChildProcess_Style9() {

    std::string strCmdLine = "/bin/cat";
    boost::asio::io_service m_oIOS;
    std::vector<char> m_oAsyncBuffer_Out;
    bp::async_pipe m_oAsyncPipe_Out(m_oIOS);
    std::error_code build_ec;

    size_t nReadSize(0);
    boost::scoped_ptr<boost::process::child> m_pChildProcess(nullptr);

    std::vector<std::string> const args = { "/home/sehe/Projects/stackoverflow/test.cpp" };
    m_pChildProcess.reset(new bp::child(strCmdLine, args, bp::std_out > m_oAsyncPipe_Out, build_ec, m_oIOS));

    std::cout << "Launched: " << build_ec.message() << std::endl;

    m_oAsyncBuffer_Out.resize(1024 * 8);

    boost::asio::async_read(m_oAsyncPipe_Out, boost::asio::buffer(m_oAsyncBuffer_Out),
        [&](const boost::system::error_code &ec, std::size_t size) {
            std::cout << "read completion handler: size = " << size << " (" << ec.message() << ")" << std::endl;
            nReadSize = size;
        });

    std::cout << "read started" << std::endl;
    size_t iii = m_oIOS.run();

    std::cout << "io_service stopped" << std::endl;
    std::cout << "initiate child::wait" << std::endl;
    m_pChildProcess->wait();
    std::cout << "wait completed" << std::endl;

    std::string const strBuf(m_oAsyncBuffer_Out.data(), nReadSize);

    int result = m_pChildProcess->exit_code();

    m_oAsyncPipe_Out.close();

    m_oIOS.reset();

    return result;
}

int main() {
    CallChildProcess_Style9();
}

印刷

http://coliru.stacked-crooked.com/a/8a9bc6bed3dd5e0a

Launched: Success
read started
read completion handler: size = 1589 (End of file)
io_service stopped
initiate child::wait
wait completed

挂断孩子

即使是固定的,也async_pipe::async_read只会读取直到缓冲区已满或到达 EOF。如果子进程输出超过缓冲区大小(示例中为 8k),那么它将被卡住并且永远不会完成。

例如:像这样替换命令:

std::string strCmdLine = "/usr/bin/yes";

结果是

Live On Coliru

Launched: Success
read started
read completion handler: size = 8192 (Success)
io_service stopped
initiate child::wait

它会一直挂到无穷远。这不是因为yes有无限的输出。任何具有大输出的命令都会挂起(例如/bin/cat /etc/dictionaries-common/words以相同的方式挂起)。您可以通过查看 strace 输出来证明这一点:

$ sudo strace -p $(pgrep yes)

strace: Process 21056 attached
write(1, "/home/sehe/Projects/stackoverflo"..., 8170

“修复”此问题的最简单方法是在填满输出缓冲区后关闭输出接收器:

boost::asio::async_read(m_oAsyncPipe_Out, boost::asio::buffer(m_oAsyncBuffer_Out),
    [&](const boost::system::error_code &ec, std::size_t size) {
        std::cout << "read completion handler: size = " << size << " (" << ec.message() << ")" << std::endl;
        nReadSize = size;
        m_oAsyncPipe_Out.close();
    });

这要求您预期孩子在您调用之前退出,wait()因此wait()可能会失败:

Live On Coliru

Launched: Success
read started
read completion handler: size = 8192 (Success)
io_service stopped
initiate child::wait
wait completed (Success)

退后一步:你需要什么?

但是,看起来您可能会变得复杂。如果您乐于将输出限制为 8k,并且您只需要拥有多个副本,为什么还要使用异步 io?

Anychild已经是异步的,你可以只传递缓冲区:

Live On Coliru

#include <boost/asio.hpp>
#include <boost/process.hpp>
#include <iostream>

namespace bp   = boost::process;
using Args     = std::vector<std::string>;
using Buffer8k = std::array<char, 8192>;

int main() {
    auto first_out  = std::make_unique<Buffer8k>(),
         second_out = std::make_unique<Buffer8k>();

    *first_out = {};
    *second_out = {};

    boost::asio::io_service svc;

    bp::child first("/bin/echo", Args{"-n", "first"}, bp::std_out > boost::asio::buffer(*first_out), svc);
    bp::child second("/bin/echo", Args{"-n", "second"}, bp::std_out >boost::asio::buffer(*second_out), svc);

    std::cout << "Launched" << std::endl;

    svc.run();
    first.wait();
    second.wait();

    std::string const strFirst(first_out->data()); // uses NUL-termination (assumes text output)
    std::string const strSecond(second_out->data()); // uses NUL-termination (assumes text output)

    std::cout << strFirst << "\n";
    std::cout << strSecond << "\n";

    return first.exit_code();
}

印刷

Launched
first
second

更多示例

因为我不能确定您需要什么,请查看我编写的其他示例以实际显示实时异步 IO,您可能需要在其中响应一个进程的特定输出。

于 2018-03-25T00:22:23.537 回答