【问题标题】:Boost::Process pipe chainBoost::Process 管道链
【发布时间】:2018-04-27 16:08:20
【问题描述】:

使用这个问题simultaneous read and write to child's stdio using boost.process 的结果,我正在尝试修改代码,以便读取文件,通过 gzip 管道传输,通过 bzip2 管道传输 gzip 的输出,最后将 bzip2 的输出写入文件。

我的第一次尝试是

/*
 * ProcessPipe.cpp
 *
 *  Created on: Apr 17, 2018
 *      Author: dbetz
 */

//#define BOOST_ASIO_ENABLE_HANDLER_TRACKING 1

#include <boost/asio.hpp>
#include <boost/asio/high_resolution_timer.hpp>
#include <boost/process.hpp>
#include <boost/process/async.hpp>
#include <iostream>
#include <fstream>
#include <functional>

namespace bp = boost::process;
using boost::system::error_code;
using namespace std::chrono_literals;

using Loop = std::function<void()>;
using Buffer = std::array<char, 500>;

int main() {
    boost::asio::io_service svc;    
    auto gzip_exit = [](int code, std::error_code ec) {
            std::cout << "gzip exited " << code << " (" << ec.message() << ")\n";
        };

    auto bzip2_exit = [](int code, std::error_code ec) {
            std::cout << "bzip2 exited " << code << " (" << ec.message() << ")\n";
        };

    bp::async_pipe file_to_gzip_pipe{svc}, gzip_to_bzip_pipe{svc}, bzip_to_file_pipe{svc};
    bp::child process_gzip("/usr/bin/gzip", "-c", bp::std_in < file_to_gzip_pipe, bp::std_out > gzip_to_bzip_pipe, svc, bp::on_exit(gzip_exit));
    bp::child process_bzip2("/usr/bin/bzip2", "-c", bp::std_in < gzip_to_bzip_pipe, bp::std_out > bzip_to_file_pipe, svc, bp::on_exit(bzip2_exit));

    std::ifstream ifs("src/ProcessPipe2.cpp");

    Buffer file_to_gzip_buffer;
    Loop file_to_gzip_loop;
    file_to_gzip_loop = [&] {
        if (!ifs.good())
        {
            error_code ec;
            file_to_gzip_pipe.close(ec);
            std::cout << "Read file, write gzip: closed stdin (" << ec.message() << ")\n";
            return;
        }

        ifs.read(file_to_gzip_buffer.data(), file_to_gzip_buffer.size());

        boost::asio::async_write(file_to_gzip_pipe, boost::asio::buffer(file_to_gzip_buffer.data(), ifs.gcount()),
            [&](error_code ec, size_t transferred) {
                std::cout << "Read file, write gzip: " << ec.message() << " sent " << transferred << " bytes\n";
                if (!ec) {
                    file_to_gzip_loop(); // continue writing
                }
            });
    };

    Buffer gzip_to_bzip_buffer;
    Loop gzip_to_bzip_loop;

    gzip_to_bzip_loop=[&] {
    gzip_to_bzip_pipe.async_read_some(boost::asio::buffer(gzip_to_bzip_buffer),
            [&](error_code ec, size_t transferred){
                // duplicate buffer
                std::cout << "Read gzip, write bzip: " << ec.message() << " got " << transferred  << " bytes\n";
                if (!ec)
                    gzip_to_bzip_loop();
                else
                    gzip_to_bzip_pipe.close();
            }
        );
    };


    std::ofstream ofs("src/ProcessPipe2.gz");

    Buffer bzip_to_file_buffer;
    Loop bzip_to_file_loop;
    bzip_to_file_loop = [&] {
    bzip_to_file_pipe.async_read_some(boost::asio::buffer(bzip_to_file_buffer),
            [&](error_code ec, size_t transferred) {
                std::cout << "Read bzip, write file: " << ec.message() << " got " << transferred << " bytes\n";
                ofs << std::string(bzip_to_file_buffer.data(),transferred);
                if (!ec)
                    bzip_to_file_loop(); // continue reading
            });
    };

    file_to_gzip_loop(); // async
    gzip_to_bzip_loop();
    bzip_to_file_loop(); // async

    svc.run(); // Await all async operations
}

但这会报错:

读取 gzip,写入 bzip:错误的文件描述符得到 0 字节

问题似乎是 gzip_to_bzip_pipe 已打开以供 gzip 写入和 bzip 读取。有什么想法吗?

【问题讨论】:

    标签: c++ asynchronous boost


    【解决方案1】:

    我会这样写代码:

    #include <boost/process.hpp>
    #include <iostream>
    
    namespace bp = boost::process;
    
    int main() {
        bp::pipe intermediate;
    
        bp::child process_gzip("/bin/gzip",   "-c", bp::std_in<"src/ProcessPipe2.cpp",   bp::std_out> intermediate);
        bp::child process_bzip2("/bin/bzip2", "-c", bp::std_in<intermediate, bp::std_out> "src/ProcessPipe2.gz.bz2");
    
        process_bzip2.wait();
        process_bzip2.wait();
    }
    

    奖金

    你可以完全不用子进程,只使用boost::iostreams::copy

    #include <boost/iostreams/filtering_stream.hpp>
    #include <boost/iostreams/filter/bzip2.hpp>
    #include <boost/iostreams/filter/gzip.hpp>
    #include <boost/iostreams/copy.hpp>
    #include <iostream>
    #include <fstream>
    
    namespace io = boost::iostreams;
    
    int main() {
        std::ifstream ifs("src/ProcessPipe2.cpp");
    
        io::filtering_stream<io::output> os;
        os.push(io::gzip_compressor());
        os.push(io::bzip2_compressor());
    
        std::ofstream ofs("src/ProcessPipe2.gz.bz2");
        os.push(ofs);
    
        io::copy(ifs, os);
    }
    

    【讨论】:

    • 我很欣赏我们提供的解决方案。 boost 库提供了许多实现目标的方法。然而,我在我的代码中使用异步管道的原因是我需要能够计算传输的字符数,而不会将整个源文件或目标文件保存在内存中(它可能高达 100 gig)。我有一个部分工作的解决方案,但我无法确定何时可以安全地调用 async_close - 即,我需要知道管道何时为空,以便待处理的操作完成且不会被取消。
    • 在管道上调用 close 将取消挂起的操作,因此这应该不是一个大问题。要检测 EOF,请继续阅读,直到收到 boost::asio::error::eof
    • 这是示例中的错字吗,wait() 的两个调用都在 process_bzip2 而不是 process_gzip 上的一个?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-21
    • 1970-01-01
    • 1970-01-01
    • 2018-07-06
    • 1970-01-01
    相关资源
    最近更新 更多