node.js 大量请求抛出错误
node.js large number of requests throwing error
我的图像下载器脚本遇到了一些问题,所以我有一个带有图像名称的数组(大小约为 5000 个元素),我在它的数组中循环,并为每个迭代下载带有请求模块的图像。
一切正常,但只有那时我的数组大小不超过 500+- 个元素。
如果我 运行 包含 5000 多个元素的脚本,我会看到请求模块(错误或未定义的响应对象)垃圾邮件中出现许多错误,最终所有应用程序都可能因 EMPTY FILE ERROR 而失败。我认为有一些异步问题导致 NODE.JS 一次没有处理那么多操作。
也许我可以通过将 5000 大小的大数组分成 300 个项目来解决这个问题,并且不要在前一个块之前的下一个块上迭代(也不要调用 fetchImage())。或者也许有更好的方法来解决我的问题。?
products.map(function (product) {
fetchImage(product.imageUrl,'./static/' + product.filename + '_big.', 0, 0);
return;
});
function fetchImage(url, localPath, index, iteration) {
var extensions = ['jpg', 'png', 'jpeg', 'bmp' , ''];
if (iteration > 2 || index === extensions.length) { // try another extensions
iteration++;
if(iteration < 3) {
setTimeout(function(){
fetchImage(url, localPath, 0, iteration);
}, 3000);
}else{
console.log('Fetching ' + url + ' failed or no image exists ');
return;
}
return;
}
var fileExtension;
if(extensions[index] === '' ) {
fileExtension = extensions[0];
}else{
fileExtension = extensions[index];
}
request.get(url + extensions[index], function(err,response,body) {
if(err || undefined === response){ // if err try after 3 sec timeout
setTimeout(function(){
console.log('Error URL : ' + url + extensions[index]);
fetchImage(url, localPath, index, iteration);
}, 3000);
return;
}else{
if(response.statusCode === 200) {
request(url + extensions[index])
.on('error', function(err) {
console.log("ERRRRRROR " + url + extensions[index] + " " + err);
setTimeout(function(){
console.log('Error URL : ' + url + extensions[index]);
fetchImage(url, localPath, index, iteration);
}, 3000);
return;
})
.pipe(fs.createWriteStream(localPath + fileExtension ));// write image to file
console.log('Successfully downloaded file ' + localPath + fileExtension);
return;
}else {
fetchImage(url, localPath, index + 1, iteration);
}
}
});
};
在每个请求之间使用 setTimeOut 已修复
setTimeout(
function () {
fetchImage(imageUrl,'./static/' + filename + '_big.', 0, 0);
},
300 * (i + 1) // where they will each progressively wait 300 msec more each
);
我的图像下载器脚本遇到了一些问题,所以我有一个带有图像名称的数组(大小约为 5000 个元素),我在它的数组中循环,并为每个迭代下载带有请求模块的图像。
一切正常,但只有那时我的数组大小不超过 500+- 个元素。
如果我 运行 包含 5000 多个元素的脚本,我会看到请求模块(错误或未定义的响应对象)垃圾邮件中出现许多错误,最终所有应用程序都可能因 EMPTY FILE ERROR 而失败。我认为有一些异步问题导致 NODE.JS 一次没有处理那么多操作。
也许我可以通过将 5000 大小的大数组分成 300 个项目来解决这个问题,并且不要在前一个块之前的下一个块上迭代(也不要调用 fetchImage())。或者也许有更好的方法来解决我的问题。?
products.map(function (product) {
fetchImage(product.imageUrl,'./static/' + product.filename + '_big.', 0, 0);
return;
});
function fetchImage(url, localPath, index, iteration) {
var extensions = ['jpg', 'png', 'jpeg', 'bmp' , ''];
if (iteration > 2 || index === extensions.length) { // try another extensions
iteration++;
if(iteration < 3) {
setTimeout(function(){
fetchImage(url, localPath, 0, iteration);
}, 3000);
}else{
console.log('Fetching ' + url + ' failed or no image exists ');
return;
}
return;
}
var fileExtension;
if(extensions[index] === '' ) {
fileExtension = extensions[0];
}else{
fileExtension = extensions[index];
}
request.get(url + extensions[index], function(err,response,body) {
if(err || undefined === response){ // if err try after 3 sec timeout
setTimeout(function(){
console.log('Error URL : ' + url + extensions[index]);
fetchImage(url, localPath, index, iteration);
}, 3000);
return;
}else{
if(response.statusCode === 200) {
request(url + extensions[index])
.on('error', function(err) {
console.log("ERRRRRROR " + url + extensions[index] + " " + err);
setTimeout(function(){
console.log('Error URL : ' + url + extensions[index]);
fetchImage(url, localPath, index, iteration);
}, 3000);
return;
})
.pipe(fs.createWriteStream(localPath + fileExtension ));// write image to file
console.log('Successfully downloaded file ' + localPath + fileExtension);
return;
}else {
fetchImage(url, localPath, index + 1, iteration);
}
}
});
};
在每个请求之间使用 setTimeOut 已修复
setTimeout(
function () {
fetchImage(imageUrl,'./static/' + filename + '_big.', 0, 0);
},
300 * (i + 1) // where they will each progressively wait 300 msec more each
);