5

在下面的示例中,我尝试将一些数据写入子进程,该子进程处理数据并将其写入文件。关闭流后,父进程无限期地等待子进程完成。我不知道如何表明我已经完成了数据的写入,并且希望子进程停止阅读并完成它正在做的任何事情。根据文档,调用终止会发送一个SIGKILL我认为不是我想要的。

我错过了什么?我检查了这个问题,但我宁愿先尝试使实际代码与同步 IO 一起工作。

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


namespace bp = boost::process;


int main(int argc, char **argv)
{
    boost::process::opstream in{};
    boost::process::child child("/path/to/test.py", bp::std_in < in);

    in << "test1\n";
    in << "test2\n";
    in << "test3\n";
    in << std::flush;

    std::cerr << "Closing the stream…\n";
    in.close();
    std::cerr << "Waiting for the child to exit…\n";
    child.wait(); // Parent seems to hang here.

    return 0;
}

test.py 只是将数据写入文件,如下所示:

#!/usr/local/homebrew/opt/python@3.8/bin/python3

import sys

with open("/tmp/test.txt", "w") as f:
    for line in sys.stdin:
        f.write(line)
4

2 回答 2

6

检查源代码后,我发现至少在这种情况下关闭流并没有关闭关联的管道。手动执行此操作确实解决了问题:

...
in.close();
in.pipe().close();
child.wait(); // Does not hang.
于 2020-06-15T16:16:04.710 回答
2

文档警告说,对子进程使用同步 IO 容易出现死锁。

这是对异步 IO 的最小改写:

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

namespace bp = boost::process;

int main() {
    boost::asio::io_context ioc;
    bp::async_pipe in{ioc};
    bp::child child("./test.py", bp::std_in < in, bp::std_out.close());

    for (auto msg : { "test1\n", "test2\n", "test3\n" }) {
        write(in, bp::buffer(msg, strlen(msg)));
    }

    std::cerr << "Closing the pipe…\n";
    in.close();
    std::cerr << "Waiting for the child to exit…\n";
    ioc.run(); // already awaits completion

    child.wait(); // Parent seems to hang here.
}

你可以通过做一些延迟使它更现实:

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

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

int main() {
    boost::asio::io_context ioc;
    bp::async_pipe in{ioc};
    bp::child child("./test.py", bp::std_in < in, bp::std_out.close());

    std::thread th([&] {
        for (auto msg : { "test1\n", "test2\n", "test3\n" }) {
            write(in, bp::buffer(msg, strlen(msg)));
            std::this_thread::sleep_for(1s);
        }

        std::cerr << "Closing the pipe…\n";
        in.close();
    });

    std::cerr << "Waiting for the child to exit…\n";
    ioc.run(); // already awaits completion
    th.join();

    child.wait(); // Parent seems to hang here.
}

对于成熟的异步 IO,请参见其他示例:

于 2020-06-15T15:57:49.257 回答