php如何实现流输出,php – 在流中读取实时整个输出

注意:这篇文章与这个

post有区别,其接受的答案只是一次读取每一行.

我必须在服务器端切割用于3D打印的3D模型,这个过程将耗费一些时间.所以我必须为用户显示进程,我使用redis来存储进程.我想每0.5秒刷新一次这个过程.

例如,睡眠0.5秒,读取点中的所有内容并每次处理它.

现在我尝试了以下两个,第一个将持续到完成.第二次使用虽然不是一种正确的方法,但它会不断写入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();

}

}

最佳答案 使用fread()替换fgets().

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值