有人能说,为什么下面的代码挂在fwrite($pipes[0], $data);上,但当我将$bytesCount更改为例如1000时,它不会挂起?
我无法通过谷歌找到答案:(
谢谢您.

$descriptorspec = array(
    0 => array("pipe", "r"),
    1 => array("pipe", "w")
);

$bytesCount = 1000000;
$process = proc_open('cat', $descriptorspec, $pipes);
$data = str_repeat('a', $bytesCount);
fwrite($pipes[0], $data);
fclose($pipes[0]);
$response = stream_get_contents($pipes[1]);
fclose($pipes[1]);
$return_value = proc_close($process);

最佳答案

管道由输入和输出缓冲区实现。cat开始读取,并将所有内容复制到输出。当输出缓冲区已满时,其写入被阻止。
由于没有任何东西在读取cat的输入(因为这一行从未到达),它将无限期地阻塞,阻塞您的fwrite

关于php - php - > fwrite来处理管道挂起 - >为什么?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37552318/

10-10 17:02