读取流中的实时整体输出 [英] Read realtime whole output in stream

查看:91
本文介绍了读取流中的实时整体输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

注意:此帖子与此被接受的帖子有所不同答案就是每次阅读每一行.

Note: this post have difference with this post whose accepted answer just read each line a time.

我必须在服务器端对3D模型进行切片以进行3D打印,该过程将花费一些时间.因此,我必须为用户展示流程,我使用redis来存储流程.我想每0.5秒刷新一次该过程. 例如,睡眠0.5秒,读取点中的所有内容,并每次都对其进行处理.

I have to slice 3D models for 3D printing on the server side, the process will cost some time. So I have to show the process for the user, I use the redis to store the process. I want to refresh the process every 0.5 seconds. For example, sleep 0.5 sec, read all the content in the pip and process it each time.

现在我尝试了以下两个,第一个将一直保持到完成为止.第二次使用虽然不是正确的方法,但它将继续写入redis,这将导致客户端读取过程请求被保留到最后.

For now I have tryed the below two, the first one will hold until it finished. the second use while is not a proper way, it will keep writing the redis will cause the client read process request hold to the end.

我尝试了这两个:

第一个将保留直到命令完成.

The first will hold until the command finished.

$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 实现的线.

and the second implemented as this post will each time one line.

  $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();
        }
    }

推荐答案

使用fread()替换fgets().

这篇关于读取流中的实时整体输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆