我有一个脚本可以从数据库中获取所有图像的URL,并通过请求模块下载这些图像。问题是当我有超过400张图像时,过程停止超过480并且不会继续。问题不在于网址,因为有时会停止超过450或467 ......
var process = {
error: 0,
success: 0,
getTotal: function() {
return this.error + this.success;
}
};
var request = require('request');
var maxLength = 10 // 10mb
var callback = function(error, success) {
if (error) {
++process.error;
} else {
++process.success;
}
console.log(process.getTotal());
};
Photo.find({limit: 500}).exec(function (err, images){
if (err || images.length === 0) {
return err;
}
for (var image in images) {
request({
url: images[image].url,
encoding: null
}, function(err, res, body) {
//console.log(file.url);
if (err) {
return callback(res, null);
}
if (res.headers['content-length'] > maxLength*1024*1024) {
return callback(new Error('Image too large.'), null)
}
if (!~[200, 304].indexOf(res.statusCode)) {
return callback(new Error('Received an invalid status code.'), null);
}
if (!res.headers['content-type'].match(/image/)) {
return callback(new Error('Not an image.'), null);
}
callback(false, true);
});
}
});
此外,我测试了限制5000张图像,并且过程停止超过4800。
答案 0 :(得分:0)
这样的事情怎么样:
var myConcurrency = 200;
var promises = [];
/**
* @returns {Promise}
*/
var getImage = function(image) {
return new Promise(resolve, reject) {
request({
//...
}, function(err, res) {
if (err) reject(err);
return resolve(res);
})
}
}
var callback = function() {};
/**
* @returns {Promise}
*/
var handleImage = function(res) {
if (res.headers['content-length'] > maxLength*1024*1024) {
return callback(new Error('Image too large.'), null)
}
if (!~[200, 304].indexOf(res.statusCode)) {
return callback(new Error('Received an invalid status code.'), null);
}
if (!res.headers['content-type'].match(/image/)) {
return callback(new Error('Not an image.'), null);
}
}
Photo.find({limit: 500}).exec(function (err, images){
if (err || images.length === 0) {
return err;
}
Promise
.map(images, function() {
return getImage();
})
.map(handleImage, {concurrency: myConcurrency});
});