本文实例分析了php进行批量任务处理不超时的解决方法。分享给大家供大家参考,具体如下:
php批量任务处理
php在批量处理任务的时候会超时,其实解决方法很简单了,就是把任务分割,一次处理一部分,任务进度可以放在服务端也可以放在客户端,不是很复杂的话放在客户端,用js来处理就可以了.
客户端js回调处理
客户端处理的时候需要住一个地方,就是使用ajax处理的时候,ajax是异步的,使用for循环来处理的时候只是批量请求,这样任务量大的时候会直接ddos服务器,所以需要等待回调函数返回,然后进行下一次的请求.
客户端例子
文件: index.html
<!doctype html>
<html>
<head>
<title></title>
<script type="text/javascript" src="http://libs.baidu.com/jquery/1.11.3/jquery.min.js"></script>
<script type="text/javascript">
$(function(){
$("#jidsall").click(function(){
$(".jids").prop("checked", this.checked);
});
$("#btn_request").click(function(){
// 任务对象
var task = {};
// 任务列表
task.list = $(".jids:checked").toarray();
// 当前任务
task.i = 0;
// 下一个请求
task.next = function() {
if (this.i >= this.list.length) {
// 任务完成
this.done();
return;
}
var i = this.i;
// 请求失败
var error = function(data){
// 失败的逻辑
console.log("error", data.id);
// 继续调用
this.next();
};
// 请求成功
var success = function(data){
// 成功的逻辑
console.log("success", data.id);
// 继续调用
this.next();
};
$.ajax({
context: this,
method: "post",
url: "do.php",
data: {id:this.list[i].value},
error: error,
success: success,
datatype: "json"
});
this.i++;
};
// 完成请求
task.done = function() {
console.log("done");
};
// 请求
task.next();
});
});
</script>
</head>
<body>
<table>
<tr><td><input type="checkbox" id="jidsall">all</td></tr>
<tr><td><input type="checkbox" value="1" class="jids">1</td></tr>
<tr><td><input type="checkbox" value="2" class="jids">2</td></tr>
<tr><td><input type="checkbox" value="3" class="jids">3</td></tr>
<tr><td><input type="checkbox" value="4" class="jids">4</td></tr>
<tr><td><input type="checkbox" value="5" class="jids">5</td></tr>
<tr><td><input type="checkbox" value="6" class="jids">6</td></tr>
<tr><td><input type="checkbox" value="7" class="jids">7</td></tr>
<tr><td><input type="checkbox" value="8" class="jids">8</td></tr>
<tr><td><input type="checkbox" value="9" class="jids">9</td></tr>
<tr><td><input type="button" id="btn_request" value="请求"></td></tr>
</table>
</body>
</html>
php处理批量任务的例子 服务端例子
文件: do.php
<?php
sleep(3);
if ($_post["id"] == 5) {
http_response_code(500);
exit();
}
echo json_encode($_post);
更多关于php相关内容感兴趣的读者可查看本站专题:《php字符串(string)用法总结》、《php数组(array)操作技巧大全》、《php运算与运算符用法总结》、《php网络编程技巧总结》、《php基本语法入门教程》、《php操作office文档技巧总结(包括word,excel,access,ppt)》、《php日期与时间用法总结》、《php面向对象程序设计入门教程》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》
希望本文所述对大家php程序设计有所帮助。
【说明】:本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:)!