shell_exec回显太快或太迟 [英] shell_exec echo'ing too fast or too late

查看:69
本文介绍了shell_exec回显太快或太迟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我通读了这篇文章: https://trac.ffmpeg.org/wiki/PHP并找到了这个

So I read throug this article: https://trac.ffmpeg.org/wiki/PHP and found this:

<?php
echo "Starting ffmpeg...\n\n";
echo shell_exec("ffmpeg -i input.avi output.avi >/dev/null 2>/dev/null &");
echo "Done.\n";

代码几乎可以正常工作,但是令我困扰的是,因为shell_exec是在后台执行的(所以整个选项卡上都没有加载符号),所有回声都被立即执行了..这意味着,在ffmpeg完成任务之前,将写入"Done"回声.

the code works almost perfectly fine, but the only thing that bothers me is, that because the shell_exec is being executed in the background (so there is no loading sign on the tab the whole time) all the echo's are being executed immediatly. This means, that the "Done" echo is being written before ffmpeg finished its task.

但是当我删除&"该站点也可以完成预期的工作,但是现在它会等待shell_exec完成,直到它回显出正在启动ffmpeg ..."

But when I remove the '&' the site does also what it is intended to do but now it waits until the shell_exec is finished until it echo's out the "Starting ffmpeg..."

所以我有点想知道如何获得这样的订单的问题:

So I kinda have the problem of figuring out how to get to such an order:

1)回声2)执行commad并等待其完成.3)再回声

1) echo something 2) execute commad and wait until its finished. 3) echo again

我是php的初学者,但有总体编程经验.请怜悯.

I am a total beginner to php but have experience in programming overall. Please have mercy.

谢谢您的时间!

推荐答案

您可能想使用 flush ,然后再执行shell_exec.有一些使用此警告的注意事项,在php文档页面中有很好的解释.希望这会有所帮助.

You might want to use flush before you do your shell_exec. There are a few caveats to using this which are well explained in the php documentation page. Hope this helps.

<?php
  echo "Starting ffmpeg...\n\n";

  ob_flush();
  flush();
  echo shell_exec("ffmpeg -i input.avi output.avi >/dev/null 2>/dev/null");

  // a sleep could effectively be the same as your shell_exec operation
  // sleep(5);

  echo "Done.\n";
  ob_end_flush();

这篇关于shell_exec回显太快或太迟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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