您好,欢迎访问一九零五行业门户网

请教大家关于php for循环中的 大数据量处理时自动exit的问题

/** * @function getsplitcache 对excelid基础数据进行分片缓存 * @param type $excelid excel文件id tax_excel的id * @param type $beforeandafterinsertid details_excel 插入前后的id * @return 无 统一excel的缓存文件标准为: excel_id_num */ public function getsplitcache($excelid, $beforeandafterinsertid){ processtip( '100%', 'excel数据分片开始.....................'); $where['id'] = array('gt', $beforeandafterinsertid['beforeid']); $where['excel_id'] = array('eq', $excelid); //是否可能存在两个表同时插入情况,如果无可省略此项 $allexceldata = $this->modeldetailsexcel->where($where)->group('nsrsbh,id')->select(); $totalnum = count($allexceldata); $this->_setallrow($totalnum);// $interval = ceil( ($beforeandafterinsertid[1]-$beforeandafterinsertid[0])/$this->exceldatacachenum);//多个人同时出现操作插入文件是可能错误 $interval = ceil($totalnum/$this->exceldatacachenum); for($i = 0; $iexceldatacachenum; $i++ ){ $exceldata[$i] = array_slice($allexceldata, $i*$interval, $interval); } unset($allexceldata); //1一个在开头一个在结尾利用时间差可以不再细分 //2必须分在一起以下为分在一起的 $i = 0; for($i; $iexceldatacachenum-1; $i++ ){ if($exceldata[$i][$interval-1]['nsrsbh'] === $exceldata[$i+1][0]['nsrsbh']){ $j = 0; while( $exceldata[$i][$interval-1]['nsrsbh'] === $exceldata[$i+1][$j]['nsrsbh']){ $exceldata[$i][] = $exceldata[$i+1][$j++]; p($j); } array_splice($exceldata[$i+1], 0, $j); } //输出前台展示进度 processtip( ($i/$this->exceldatacachenum*100), 'excel数据分片'.$i.'.....................'); //thinkphp的f缓存方法 指定位置和名称 f('excel_'.$excelid.'/'.$i , $exceldata[$i]); } f('excel_'.$excelid.'/'. $i , $exceldata[$i]); processtip( '100%', 'excel数据分片结束.....................'); return $this->exceldatacachenum; }

基本原理是 将数据库数据按照nsrsbh 和 id排序 读取出来,然后将数据按照分片数据 分割指定数目的文件(用于前台ajax请求,模拟多线程操作,js发送请求后端接收后处理传过来的数据分片的文件name,然后处理改文件内的数据)
系统环境 apche php 分配的memoryliimit win(win10 64bit 4g内存)为2g linux(ubuntu14.04 64位 8g内存)为4g
经过测试当数据量在3500条时分片1000正常执行。
当数据量在11770条时,linux下和win下开始出现这个问题
到978片缓存生成时就进行不下去,php程序自动跳出 效果如下 然后进程就退出了。
当分片为500的时候出现到488 就会卡 然后到了过个2分钟左右才会输出489 然后没有然后了 进程跳出了
回复内容: /** * @function getsplitcache 对excelid基础数据进行分片缓存 * @param type $excelid excel文件id tax_excel的id * @param type $beforeandafterinsertid details_excel 插入前后的id * @return 无 统一excel的缓存文件标准为: excel_id_num */ public function getsplitcache($excelid, $beforeandafterinsertid){ processtip( '100%', 'excel数据分片开始.....................'); $where['id'] = array('gt', $beforeandafterinsertid['beforeid']); $where['excel_id'] = array('eq', $excelid); //是否可能存在两个表同时插入情况,如果无可省略此项 $allexceldata = $this->modeldetailsexcel->where($where)->group('nsrsbh,id')->select(); $totalnum = count($allexceldata); $this->_setallrow($totalnum);// $interval = ceil( ($beforeandafterinsertid[1]-$beforeandafterinsertid[0])/$this->exceldatacachenum);//多个人同时出现操作插入文件是可能错误 $interval = ceil($totalnum/$this->exceldatacachenum); for($i = 0; $iexceldatacachenum; $i++ ){ $exceldata[$i] = array_slice($allexceldata, $i*$interval, $interval); } unset($allexceldata); //1一个在开头一个在结尾利用时间差可以不再细分 //2必须分在一起以下为分在一起的 $i = 0; for($i; $iexceldatacachenum-1; $i++ ){ if($exceldata[$i][$interval-1]['nsrsbh'] === $exceldata[$i+1][0]['nsrsbh']){ $j = 0; while( $exceldata[$i][$interval-1]['nsrsbh'] === $exceldata[$i+1][$j]['nsrsbh']){ $exceldata[$i][] = $exceldata[$i+1][$j++]; p($j); } array_splice($exceldata[$i+1], 0, $j); } //输出前台展示进度 processtip( ($i/$this->exceldatacachenum*100), 'excel数据分片'.$i.'.....................'); //thinkphp的f缓存方法 指定位置和名称 f('excel_'.$excelid.'/'.$i , $exceldata[$i]); } f('excel_'.$excelid.'/'. $i , $exceldata[$i]); processtip( '100%', 'excel数据分片结束.....................'); return $this->exceldatacachenum; }

基本原理是 将数据库数据按照nsrsbh 和 id排序 读取出来,然后将数据按照分片数据 分割指定数目的文件(用于前台ajax请求,模拟多线程操作,js发送请求后端接收后处理传过来的数据分片的文件name,然后处理改文件内的数据)
系统环境 apche php 分配的memoryliimit win(win10 64bit 4g内存)为2g linux(ubuntu14.04 64位 8g内存)为4g
经过测试当数据量在3500条时分片1000正常执行。
当数据量在11770条时,linux下和win下开始出现这个问题
到978片缓存生成时就进行不下去,php程序自动跳出 效果如下 然后进程就退出了。
当分片为500的时候出现到488 就会卡 然后到了过个2分钟左右才会输出489 然后没有然后了 进程跳出了
你去php.ini文件中看看有没有配置最大变量长度的那个设置项,找到它,修改后服务器重启即可。
我以前恰好遇到过这个问题
打开 error_reporting
可在脚本最上方添加
phperror_reporing(e_all);ini_set('display_errors');
监控数据量多的情况下,是不是内存使用超出限制
如果确认是内存溢出
改造你的程序,降低内存使用
比如使用generator
具体根据你的业务,尽量不要取太多数据放入内存
保证你程序运行的时候不要随着处理数据量的增大而内存也跟着增大
理想的情况是处理数据量的增长不会导致程序运行内存的暴增
set_time_limit(0),设置超时时间,不是内存的问题可能是超时了
其它类似信息

推荐信息