PHP proc_open实时命令输出不起作用



我正在尝试使用PHP流式传输一些脚本的实时输出。StackOverflow上有很多关于这方面的问题。我遵循了以下答案:
PHP正在读取shell_exec实时输出
从PHP执行的Bash脚本实时输出
在PHP中运行具有实时输出的进程
使用PHP exec((实时输出到文件?

但是,它们都不适合我。我总是在命令完成时得到整个输出。这是我使用的最后一个代码:

$cmd = "/path/to/command";
$descriptorspec = array(
0 => array("pipe", "r"),   // stdin is a pipe that the child will read from
1 => array("pipe", "w"),   // stdout is a pipe that the child will write to
2 => array("pipe", "w")    // stderr is a pipe that the child will write to
);
flush();
$process = proc_open($cmd, $descriptorspec, $pipes, realpath('./'), array());
echo "<pre>";
if (is_resource($process)) {
while ($s = fgets($pipes[1])) {
print $s;
flush();
}
}
echo "</pre>";

平台:使用Apache 的Arch Linux

我试图运行一个Python程序作为外部命令
我做了更多的研究,结果发现Python方面有输出缓冲,所以PHP在所有输出都由Python解释器发出之前没有得到任何输出
我在python命令中禁用了输出缓冲,现在它按预期工作。

要禁用python中的输出缓冲,只需添加-u开关:python -u my_prog.py

最新更新