注意:这篇文章与此有所不同post https://stackoverflow.com/questions/1281140/run-process-with-realtime-output-in-php其接受的答案只是阅读每一行一次。
我必须在服务器端对3D模型进行切片以进行3D打印,这个过程将花费一些时间。所以我必须向用户展示进程,我使用redis来存储进程。我想每0.5秒刷新一次进程。
比如sleep 0.5秒,每次读取pip中的所有内容并处理。
现在我已经尝试了以下两个,第一个将保留到完成为止。第二种使用while不是一个正确的方式,它会不断写redis,导致客户端读进程请求保持到结束。
我尝试过这两个:
第一个将保持直到命令完成。
$descriptorspec = array(
0 => array("pipe", "r"),
1 => array("pipe", "w"),
2 => array("pipe", "w") //here curaengine log all the info into stderror
);
$command = './CuraEngine slice -p -j ' . $fdmprinterpath . ' -j ' . $configpath . ' -o ' . $gcodepath . ' -l ' . $tempstlpath;
$cwd = '/usr/local/curaengine';
$process = proc_open($command, $descriptorspec, $pipes, $cwd);
if(is_resource($process))
{
print stream_get_contents($pipes[1]); //This will hold until the command finished.
}
第二个是这样实现的post https://stackoverflow.com/questions/1281140/run-process-with-realtime-output-in-php每次都会一行。
$descriptorspec = array(
0 => array("pipe", "r"),
1 => array("pipe", "w"),
2 => array("pipe", "w") //here curaengine log all the info into stderror
);
$command = './CuraEngine slice -p -j ' . $fdmprinterpath . ' -j ' . $configpath . ' -o ' . $gcodepath . ' -l ' . $tempstlpath;
$cwd = '/usr/local/curaengine';
$process = proc_open($command, $descriptorspec, $pipes, $cwd);
if(is_resource($process))
{
while ($s = fgets($pipes[1])) {
print $s;
flush();
}
}