gpt4 book ai didi

java - 获取继承父进程 IO 的子进程的输出

转载 作者:行者123 更新时间:2023-12-02 04:33:37 25 4
gpt4 key购买 nike

我在获取子进程的输出时遇到问题。通常我会使用process.getInputStream()。然而,在本例中,我将子进程设置为继承父进程的 IO。我需要这样做,因为子进程的输出超出了内核设置的缓冲区,因此将挂起。顺便说一句,我正在使用 Linux。

    //creating a named pipe
File fifo = fifoCreator.createFifoPipe("fifo");
String[] command = new String[] {"cat", fifo.getAbsolutePath()};
process = new ProcessBuilder(command).inheritIO().start(); //Inherit IO

FileWriter fw = new FileWriter(fifo.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
bw.write(boxString);
bw.close();
process.waitFor();
fifoCreator.removeFifoPipe(fifo.toString());

//Gets nothing here
stdInput = new BufferedReader(new InputStreamReader(process.getInputStream()));
String input = readAllLines(stdInput);

理想情况下,我希望将所有内容保留在内存中,因为我想要良好的性能和更少的垃圾。

有没有办法将子进程的输出重定向到某种数据结构?如果没有,有什么办法得到输出吗?

我正在考虑创建一个单独的线程来获取输出流。

最佳答案

为了避免内核缓冲区问题,您可以使用线程读取输出,如 this question 所示。 。他们称之为 StreamGobbler

关于java - 获取继承父进程 IO 的子进程的输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31121578/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com