这是一个解决方案,不是最好的,但在 Linux 上可以正常工作:
将处理PHP拆分成单独的CLI脚本,其中:
- 命令行输入包括`$id`和`$item`
- 脚本将其 PID 写入 `/tmp/$id.$item.pid` 中的文件
- 脚本将结果作为 XML 或可以读入 PHP 的内容回显到标准输出
- 完成后,脚本会删除 `/tmp/$id.$item.pid` 文件
您的主脚本(可能在您的网络服务器上)可以:
- `exec("nohup php myprocessing.php $id $item > /tmp/$id.$item.xml");` 对于每个项目
- 轮询 `/tmp/$id.$item.pid` 文件直到所有文件都被删除(睡眠/检查轮询就足够了)
- 如果它们从未被删除,则终止所有处理脚本并报告失败
- 如果成功从 `/tmp/$id.$item.xml` 读取格式/输出给用户
- 如果您不想缓存以供以后使用,请删除 XML 文件
后台 nohup 启动的应用程序将独立于启动它的脚本运行。
这让我很感兴趣,所以我决定写一个 POC。
test.php
<?php
$dir = realpath(dirname(__FILE__));
$start = time();
// Time in seconds after which we give up and kill everything
$timeout = 25;
// The unique identifier for the request
$id = uniqid();
// Our "items" which would be supplied by the user
$items = array("foo", "bar", "0xdeadbeef");
// We exec a nohup command that is backgrounded which returns immediately
foreach ($items as $item) {
exec("nohup php proc.php $id $item > $dir/proc.$id.$item.out &");
}
echo "<pre>";
// Run until timeout or all processing has finished
while(time() - $start < $timeout)
{
echo (time() - $start), " seconds\n";
clearstatcache(); // Required since PHP will cache for file_exists
$running = array();
foreach($items as $item)
{
// If the pid file still exists the process is still running
if (file_exists("$dir/proc.$id.$item.pid")) {
$running[] = $item;
}
}
if (empty($running)) break;
echo implode($running, ','), " running\n";
flush();
sleep(1);
}
// Clean up if we timeout out
if (!empty($running)) {
clearstatcache();
foreach ($items as $item) {
// Kill process of anything still running (i.e. that has a pid file)
if(file_exists("$dir/proc.$id.$item.pid")
&& $pid = file_get_contents("$dir/proc.$id.$item.pid")) {
posix_kill($pid, 9);
unlink("$dir/proc.$id.$item.pid");
// Would want to log this in the real world
echo "Failed to process: ", $item, " pid ", $pid, "\n";
}
// delete the useless data
unlink("$dir/proc.$id.$item.out");
}
} else {
echo "Successfully processed all items in ", time() - $start, " seconds.\n";
foreach ($items as $item) {
// Grab the processed data and delete the file
echo(file_get_contents("$dir/proc.$id.$item.out"));
unlink("$dir/proc.$id.$item.out");
}
}
echo "</pre>";
?>
proc.php
<?php
$dir = realpath(dirname(__FILE__));
$id = $argv[1];
$item = $argv[2];
// Write out our pid file
file_put_contents("$dir/proc.$id.$item.pid", posix_getpid());
for($i=0;$i<80;++$i)
{
echo $item,':', $i, "\n";
usleep(250000);
}
// Remove our pid file to say we're done processing
unlink("proc.$id.$item.pid");
?>
将 test.php 和 proc.php 放在你服务器的同一个文件夹中,加载 test.php 并享受。
你当然需要 nohup (unix) 和 PHP cli 才能让它工作。
很有趣,我以后可能会发现它的用处。