我正在节点上运行日志扫描和统计功能,由于文件大小有点大,所以执行时间很长,所以我尝试使用promise pool来增强它。下面是我的函数的简单工作流程。
const fs = require("fs");
const readline = require("readline");
const filePathList = [
".\\log1.log",
".\\log2.log",
".\\log3.log",
".\\log4.log",
".\\log5.log",
".\\log6.log",
".\\log7.log",
".\\log8.log",
".\\log9.log",
".\\log10.log",
];
const poolSize = 10;
let promisePool = new Set();
const statistic = async () => {
while (filePathList.length > 0) {
while (promisePool.size < poolSize) {
const filePath = filePathList.shift();
const promise = new Promise(async (resolve, reject) => {
try {
const fileStream = fs.createReadStream(filePath);
const readLine = readline.createInterface({
input: fileStream,
crlfDelay: Infinity,
});
for await (const line of readLine) {
// do something...
}
fileStream.destroy();
promisePool.delete(promise);
console.log(
`${filePath} DONE!, remain promises: ${promisePool.size}`
);
resolve();
analyzLog(threadIndex);
} catch (err) {
reject(err);
}
});
promisePool.add(promise);
}
await Promise.race(promisePool);
}
await Promise.all(promisePool);
};
(async () => {
const start = Date.now();
console.log(`Start statistic algorithm, timestamp: ${start}`);
await statistic();
const end = Date.now();
console.log(`End statistic algorithm, timestamp: ${end}`);
console.log(`It take ${(end - start) / 1000} sec`);
})();
据我所知,文件系统是在节点外运行的,而且是多线程的。所以我希望它能快十倍。但无论我使用池大小10还是1运行它。它花费了类似的时间成本,所以我想我没有真正做分析的一部分同时进行。我怎么才能让它工作?
1条答案
按热度按时间41zrol4v1#
你只能用
Promise.all
来实现这一点。将每个任务放入单个promise中,然后将其推入数组中,然后执行Promise.all(array)
,以便所有任务并行运行。输出: