Node.js 承诺履行问题。读取文件太慢
我正在使用承诺读取硬盘中的10000多个文件,并在精确位置找到一个数字。我使用glob返回文件名,对于找到的每个文件,我都运行readFile方法(readFile作为promisse返回)。处理完所有文件后,我可以继续工作Node.js 承诺履行问题。读取文件太慢,node.js,io,promise,bluebird,Node.js,Io,Promise,Bluebird,我正在使用承诺读取硬盘中的10000多个文件,并在精确位置找到一个数字。我使用glob返回文件名,对于找到的每个文件,我都运行readFile方法(readFile作为promisse返回)。处理完所有文件后,我可以继续工作 function readFilesGlob(globPath,options,progressCallback){ return new Promise(function (fulfill, reject){ glob(globPath, optio
function readFilesGlob(globPath,options,progressCallback){
return new Promise(function (fulfill, reject){
glob(globPath, options, function (err, files) {
var readPromisses=[];
for(var id in files){
readPromisses.push(readFile(files[id]));
}
Promise.all(readPromisses).then(
function(filesContents){
fulfill(filesContents)
}
);
});
});
}
所有承诺只有在完成所有工作后才能完成,因此无法显示处理进度
function readFilesGlob(globPath,options,progressCallback){
return new Promise(function (fulfill, reject){
glob(globPath, options, function (err, files) {
var readPromisses=[];
for(var id in files){
readFile(files[id]).then(function(data){
//everything shows at the same time, like i was using the Promise.all
console.log(data)
})
}
//just testing the speed of the return in the console.log above
fulfill();
});
});
问题是。它太慢了,我只有几分钟后才能返回(或者当我记忆犹新时)
我觉得我使用的承诺是错误的。有人能给我一个更有效的例子来阅读带有承诺的文件列表吗?我认为在for循环中,您需要这样的东西
readPromisses.push(
new Promise( function(y,n) { readFile(files[id]); y();} )
)
然后,当异步执行Promise
时,将调用readFile
。
这只是一个建议,我还没有测试过,所以你可能需要调整一下
这是一个骨架,也许会有帮助
var firstCall = new Promise((y, n) => setTimeout( () => y(Math.random()), 500 ));
var secondCall = (arg) => {
return new Promise((y, n) => setTimeout( () => y(Math.random() + ' ' + arg), 200 ));
}
var rejectCall = () => new Promise((y, n) => setTimeout( () => n(true), 400 ));
Promise.all([
firstCall,
secondCall('withArg'),
]).then( (data) => {
console.log( 'ALL: First Call result:', data[0]);
console.log( 'ALL: Second Call result:', data[1]);
}).catch( (err) => {
console.log( 'Promise.all caught reject or error:', err);
});
这似乎是一个很好的解决方案的包 签出每个
功能:
示例:
// assuming openFiles is an array of file names
async.each(openFiles, function(file, callback) {
// Perform operation on file here.
console.log('Processing file ' + file);
if( file.length > 32 ) {
console.log('This file name is too long');
callback('File name too long');
} else {
// Do work to process file here
console.log('File processed');
callback();
}
}, function(err) {
// if any of the file processing produced an error, err would equal that error
if( err ) {
// One of the iterations produced an error.
// All processing will now stop.
console.log('A file failed to process');
} else {
console.log('All files have been processed successfully');
}
});
我在几乎所有的项目中都使用异步包,它速度快,功能丰富如果是I/O问题,您可以将“文件”数组分割成块,并链接这些块
chunks.reduce((result, chunk) => {
return Promise.all(chunk.map(file=> promiseRead(file))
.then(results => dealWithChunkResults(results)
}, Promise.resolve())
Promise.all
将等待每个文件被读取,而超时示例只读取一个文件。在我看来,您的性能问题不是关于承诺,而是关于同时阅读大量文件。谢谢,超时使我只能使用最后一个承诺。我做了另一个测试,没有使用Promise.all。只需将加载的文件记录到console.log。它们只在加载所有内容时列出我注意到您正在读取10000多个文件,并且带有Promise。所有1000+承诺都不是一个很好的实现方式,尽管它不仅需要i/o,而且还需要CPU和RAM。依我看,如果同时限制读取文件的数量,效果会更好。e、 g:一次500个文件或1000个。我认为它可以提高速度,也比全部阅读节省资源。最后还是什么都做。我想知道这是否与I/O有关?啊,我想问题可能出在您的.then
条款中。据我所知,。然后,
将按调用顺序接收结果数组。让我找到用于此的框架。async。每个
都不能解决问题,因为这也会并行处理所有文件。相反,请使用类似的方法。