简体   繁体   English

Boost ::工艺管链

[英]Boost::Process pipe chain

Using the results of this question simultaneous read and write to child's stdio using boost.process , I am trying to modify the code so that a file is read, piped through gzip, the output of gzip piped through bzip2, and finally the output of bzip2 written to a file. 使用这个问题的结果, 同时使用boost.process读写孩子的stdio ,我试图修改代码,以便读取文件,通过gzip传递管道,通过gzip 2传递管道gzip的输出,最后通过bzip2传递输出写入文件。

My first attempt was 我的第一次尝试是

/*
 * 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
}

but this gives an error: 但这给出了一个错误:

Read gzip, write bzip: Bad file descriptor got 0 bytes 读取gzip,写入bzip:错误的文件描述符得到0个字节

The problem seems to be that gzip_to_bzip_pipe is opened for writing by gzip and for reading by bzip. 问题似乎是打开了gzip_to_bzip_pipe以便通过gzip编写和通过bzip读取。 Any ideas? 有任何想法吗?

I'd write that code simply like: 我会像这样写代码:

#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();
}

BONUS 奖金

You can do without sub processes entirely and just use boost::iostreams::copy : 您可以完全不用子进程,而只需使用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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 提升过程中的管道缓冲区大小 - Pipe buffer size in boost process Boost::Process Pipe 流和单元测试 - Boost::Process Pipe Streams and Unit Test Boost async_pipe 不显示所有子进程输出 - Boost async_pipe not showing all child process output 当stderr重定向到管道时,为什么boost :: process在Windows上崩溃? - Why does boost::process crash on Windows when stderr is redirected to a pipe? 使用 Boost C++ 使用管道将输出从子进程发送到父进程(将标准输出重定向到管道) - Sending output from child process to parent using pipe (redirecting stdout to pipe) using Boost C++ boost::range 处理容器的内容并使用映射中的值管道到下一个进程 - boost::range to process content of a container and pipe to next process with values from a map 使用 boost::asio::async_write 和 boost::process::async_pipe 多次写入子进程的标准输入 - Using boost::asio::async_write and boost::process::async_pipe to write to a child process's stdin multiple times 连接到提升命名管道 - Connect to boost named pipe 如何有效地使用 boost::process::async_pipe 进行读写? - How can I effectively use boost::process::async_pipe for both writing and reading? 带有命名管道的 boost::asio; 父进程无法通过 async_read_some 从 pipe 读取 - boost::asio with named pipes; parent process can't read from pipe via async_read_some
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM