防止在 PHP 中的大请求期间超时
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3909191/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Prevent timeout during large request in PHP
提问by Dave Kiss
I'm making a large request to the brightcove servers to make a batch change of metadata in my videos. It seems like it only made it through 1000 iterations and then stopped - can anyone help in adjusting this code to prevent a timeout from happening? It needs to make about 7000/8000 iterations.
我正在向 Brightcove 服务器发出大量请求,要求对我的视频中的元数据进行批量更改。似乎它只通过了 1000 次迭代然后停止了 - 任何人都可以帮助调整此代码以防止超时发生吗?它需要进行大约 7000/8000 次迭代。
<?php
include 'echove.php';
$e = new Echove(
'xxxxx',
'xxxxx'
);
// Read Video IDs
# Define our parameters
$params = array(
'fields' => 'id,referenceId'
);
# Make our API call
$videos = $e->findAll('video', $params);
//print_r($videos);
foreach ($videos as $video) {
//print_r($video);
$ref_id = $video->referenceId;
$vid_id = $video->id;
switch ($ref_id) {
case "":
$metaData = array(
'id' => $vid_id,
'referenceId' => $vid_id
);
# Update a video with the new meta data
$e->update('video', $metaData);
echo "$vid_id updated sucessfully!<br />";
break;
default:
echo "$ref_id was not updated. <br />";
break;
}
}
?>
Thanks!
谢谢!
回答by bobdiaes
Try the set_time_limit()function. Calling set_time_limit(0)
will remove any time limits for execution of the script.
试试set_time_limit()函数。调用set_time_limit(0)
将删除执行脚本的任何时间限制。
回答by Billy
Also use ignore_user_abort()to bypass browser abort. The script will keep running even if you close the browser (use with caution).
还可以使用ignore_user_abort()绕过浏览器中止。即使您关闭浏览器,脚本也会继续运行(谨慎使用)。
回答by Robidu
Try sending a 'Status: 102 Processing' every now and then to prevent the browser from timing out (your best bet is about 15 to 30 seconds in between). After the request has been processed you may send the final response.
尝试不时发送“状态:102 正在处理”以防止浏览器超时(您最好的选择是间隔大约 15 到 30 秒)。处理完请求后,您可以发送最终响应。
The browser shouldn't time out any more this way.
浏览器不应再以这种方式超时。