在大文件上传中批量处理数据时,如何避免PHP / WordPress内存致命错误?

Aurovrata

我有一个很大的CSV文件,我将其上传到WordPress仪表板以导入分类法术语。我写了一个小插件,使用wp_insert_term()函数插入每个术语,但是,该函数缓存了大量数据以检查段的唯一性和父术语依赖性,尽管增加了对0.5 Gb。

我一直想将文件拆分为可管理的块,以便对数据进行批处理并运行限制为1000或几行数据的会话,这样每个进程都会干净地终止。

我一直在寻找这样的解决方案,并发现了这篇有趣的文章,涉及批量映像导入面临的类似问题,它概述了开发人员如何使用javascript通过向服务器和可管理块发送ajax请求来控制批处理过程。

它使我想到了在上载时读取CSV文件,逐行读取并将ajax请求发送到服务器以处理可管理的行数的想法

这是更好的方法吗?

Aurovrata

我根据问题中的链接和其他一些修补方法,开发了以下解决方案。

在WordPress服务器端,加载javascript文件时,我根据内存分配情况确定服务器可以处理的行数,

$limit = ini_get('memory_limit');
$limit = wp_convert_hr_to_bytes($limit) / MB_IN_BYTES; //in MBs.
switch(true){
    case $limit >= 512:
        $limit = 1000;
        break;
    default:
        $limit = 500;
        break;
}
wp_enqueue_script( 'my-javascript-file');
wp_localize_script( 'my-javascript-file', 'cirData', array(
    'limit'=>$limit
));

您应该根据自己的过程确定并设置自己的限制。

在javascript文件中,使用jQuery,

var reader,formData, lineMarker=0, csvLines, isEOF=false, $file, $form ;
  $(document).ready(function(){
    $file = $(':file'); //file input field
    $form = $('form');  //form
    //when the file field changes....
    $file.on('change', function(){
      //check if the file field has a value.
      if($file.val()){
        //setup file reader.
        reader = new FileReader();
        //now listen for when the file is ready to be read.
        reader.addEventListener('load', function (e) {
          csvLines = e.target.result.split("\n");
          batchProcess(); //launch process.
        });
        //when the form is being submitted, start reading the file.
        $(document).on('click', ':submit', function(e){
          e.preventDefault(); //disable normal submit.
          //setup data for the ajax.
          formData = new FormData($form.get(0));

          //read the file and batch request to server.
          reader.readAsBinaryString($file.get(0).files[0]);
        })
      }
    })
  });

  // Methods
  //posting
  function postCSVdata(csvdata){
    formData.set('csvlines', csvdata); //set the current datat to send.
    $.ajax({
      type: 'POST',
      url: $form.attr('action'),
      data: formData,
      contentType: false,
      processData: false,
      cache: false,
      success: function(data){
        var msg ="";
        if(isEOF){ //is this end of the file?
          console.log("success!");
        }else{ //continue reading file.
          console.log("uploaded:"+ Math.round(lineMarker/csvLines.length*100)+"%");
          batchProcess(); //process the next part of the file.
        }
      }
    })
  }
  //batch process.
  function batchProcess(){
    //csvlines is the array containing all the lines read from the file.
    //lineMarker is the index of the last line read.
    var parsedata='', stop = csvLines.length - lineMarker, line='';

    for(var i = 0; i < stop; i++) {
      line = csvLines[i+lineMarker];
      parsedata +=line+"\n"; //add a new line char for server to process.
      //check if max limit of lines server can process is reached.
      if(i>(cirData.limit-2)) break; //batch limit.
    }
    lineMarker += i;
    if(i==stop) isEOF = true;
    postCSVdata(parsedata); //send to server.
  }

这会以顺序方式在服务器能够处理的几行行中发送多个AJAX请求,而不会出现致命的内存错误。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

致命错误 - 允许的内存 - PHP

上传大文件 - 错误 VirtualAlloc of x 字节失败,errno=1455 致命错误:内存不足

更新 wordpress 时出现致命错误

使用Ruby将大文件上传到S3时出现内存不足错误,如何批量读取和上传?

如何解决wordpress的致命错误?

PHP图像调整大小致命错误:内存不足

如何解决Wordpress 404错误页面中的致命错误?

如何删除文件上传中的错误消息?

在内存有限的系统上写入大文件时,如何避免mapFailed()错误

GWT:如何从多部分表单(文件)上传中返回(和处理)错误

添加行操作时发生致命错误(Wordpress)

致命错误:我的PHP脚本允许的内存大小(ams)和内存不足(oom)

尝试迁移时,PHP致命错误出现内存不足错误

分析大文件中的数据时如何使用最小内存

从文件上传中获取错误

进程内存不足后返回Angular API致命错误处理程序

wp-db.php中的Wordpress内存不足错误

致命错误 134217728 字节的允许内存大小在 MySQL 查询表中的总量时耗尽

使用条件语句循环时,致命错误导致内存耗尽

Java 致命错误文件显示内存不足而堆转储小得多

Composer require内存不足。PHP致命错误:允许的内存大小为1610612736字节已用尽

Symfony缓存:清除php。严重:致命错误:允许的内存大小

PHP 致命错误:允许的内存大小为 2097152 字节已用完(尝试分配 12288 字节)

Symfony 5缓存:清除php。严重:致命错误:允许的内存大小

PHP 致命错误:composer 更新允许的内存大小为 1610612736 字节

从iPhone上传文件导致PHP致命错误

Wordpress错误PHP可捕获的致命错误:WP_Term类的对象

内存中的致命错误

如何在 WordPress 中为致命错误处理程序 (WSoD) 设置通知电子邮件