【发布时间】:2017-05-06 09:09:31
【问题描述】:
注意:这篇文章与post 不同,后者接受的答案只是一次阅读每一行。
我必须在服务器端对 3D 模型进行切片以进行 3D 打印,这个过程会花费一些时间。所以我要给用户展示流程,我用redis来存储流程。我想每 0.5 秒刷新一次进程。 比如sleep 0.5 sec,读取pip中的所有内容,每次处理。
目前我已经尝试了以下两个,第一个将一直保持到完成。第二次使用虽然不是正确的方法,它会一直写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 将每次一行。
$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();
}
}
【问题讨论】:
-
这是为了显示切片过程的某种进展吗?另外,您将使用什么客户端?浏览器?
-
@Ray Radin 是的,它是一个网络云服务,用户可以使用我的服务器对 3D 模型进行切片,生成用于 3D 打印的 gcode。第一个工具将在完成时输出所有内容。 secnod 实现将一直保持 printint 到最后。但正如问题所说,我想每 0.5 秒处理一次。
-
我写了一个应用程序部署器,它更新一个进度条来显示部署进度。如果您愿意稍微修改您的方法,我可以向您展示它的精简版本作为示例。
-
再想一想,这可能不是您这个问题的正确答案。
-
你可以展示一下,因为没有人回答这个问题,我可能会从你所做的事情中学到一些东西。谢谢!
标签: php process stream pipe real-time