异步运行PHP exec(),但检查是否完成


Run PHP exec() asynchronously, but check for completion?

我正在编写一个网站后端,将包括用户上传的视频。为了确保最大的可访问性,我压缩了上传的视频,并将它们重新保存为。mp4和。webm格式,以覆盖所有浏览器(或者尽可能多的浏览器)。为此,我在PHP的exec()函数中运行avconv命令。

我不想让用户在页面加载之前等待脚本完成,所以我异步运行代码。到目前为止,我的代码如下:

exec('bash -c "exec nohup setsid avconv -i ' . $tempPath . ' -c:v libx264 ' . $transpose . ' ' . $newPath . 'mp4 > /dev/null 2>/dev/null &"');
exec('bash -c "exec nohup setsid avconv -i ' . $tempPath . ' -c:v libvpx ' . $transpose . ' ' . $newPath . 'webm > /dev/null 2>/dev/null &"');

除了运行exec函数之外,我还将视频保存到数据库中,并向用户发送电子邮件,感谢他们上传视频。

问题是:我希望服务器等待视频转换完成,然后将其添加到数据库并向用户发送电子邮件。基本上,程序流将是:

用户上传视频。视频被放置在临时文件夹中。用户被带到一个感谢页面,表示他们的视频将很快发布。服务器执行两个avconv命令来转换和压缩视频以供web使用。一旦两个转换完成,视频信息被添加到MySQL数据库中,电子邮件被发送给用户,原始上传的视频被删除。

这可能只是我对命令行的无知(事实上它几乎肯定是),但是我怎么能"排队"这些命令?首先进行两种转换,然后调用PHP脚本向数据库添加视频,然后删除原始视频,所有这些都是与原始PHP脚本异步进行的。

编辑:我已经尝试用'&&'操作符将它们排队,如下所示:

exec('bash -c "exec nohup setsid avconv -i ' . $tempPath . ' -c:v libx264 ' . $transpose . ' ' . $newPath . 'mp4 && avconv -i ' . $tempPath . ' -c:v libvpx ' . $transpose . ' ' . $newPath . 'webm > /dev/null 2>/dev/null &"');

然而,这似乎抵消了我异步运行它的事实,因为页面现在似乎等待命令完成。

你应该启动一个异步的命令行php脚本,对视频和进行编码,然后发送一个电子邮件:

upload.php:

exec('/usr/bin/php -f encode_files.php > /dev/null 2>/dev/null &"');
echo "Files will be encoded, come back later !";

encode_files.php

exec('avconv ...'); // Synchronously ! Without > /dev/null etc ...
exec('avconv ...'); // webm ...
mail('user@user.com', 'Encoding complete ! ', 'Great ! ');

我离开了调用"bash -c exec…",但我认为有更短的方法来异步调用php脚本:PHP中的异步shell执行您甚至可以传递参数(如用户/视频id,…)

$cmd = 'nohup /usr/bin/php -f /path/to/php/file.php action=generate var1_id=23 var2_id=35 gen_id=535 > /path/to/log/file.log & printf "%u" $!';
$pid = shell_exec($cmd);

您可以断开PHP脚本与客户端的连接,但让它继续运行以完成您的任务。

// Your preliminary stuff here ...
/// then send the user elsewhere but carry on in the background
ignore_user_abort(true);
set_time_limit(0); // i.e. forever
header("Location: thankyoubutwait.php", true);
header("Connection: close", true);
header("Content-Encoding: none'r'n");
header("Content-Length: 0", true);
flush();
ob_flush();
session_write_close();
// more of your video stuff here including database writes
// and clean up bits
// (you may end up with zombie processes though so check your logs or write statuses to files etc.)

这很简单,您只需要检查命令行是否执行良好,就像这样:

// Your code before...
$command = 'bash -c "exec nohup setsid avconv -i ' . $tempPath . ' -c:v libx264 ' . $transpose . ' ' . $newPath . 'mp4 > /dev/null 2>/dev/null &"'
exec($command, $return, $status);
if($status == 0 ) {
    $command2 = 'bash -c "exec nohup setsid avconv -i ' . $tempPath . ' -c:v libvpx ' . $transpose . ' ' . $newPath . 'webm > /dev/null 2>/dev/null &"';
    exec($command2, $return2, $status2);
    if($status2==0){
        // let your user know your video traitement has been done
        // lauch a new function for alert him
    }
}

// Kill your process at end
die();