我有一个很大的CSV文件,我将其上传到WordPress仪表板以导入分类法术语。我写了一个小插件,使用wp_insert_term()
函数插入每个术语,但是,该函数缓存了大量数据以检查段的唯一性和父术语依赖性,尽管增加了对0.5 Gb。
我一直想将文件拆分为可管理的块,以便对数据进行批处理并运行限制为1000或几行数据的会话,这样每个进程都会干净地终止。
我一直在寻找这样的解决方案,并发现了这篇有趣的文章,涉及批量映像导入面临的类似问题,它概述了开发人员如何使用javascript通过向服务器和可管理块发送ajax请求来控制批处理过程。
它使我想到了在上载时读取CSV文件,逐行读取并将ajax请求发送到服务器以处理可管理的行数的想法。
这是更好的方法吗?
我根据问题中的链接和其他一些修补方法,开发了以下解决方案。
在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] 删除。
我来说两句