Skip to content

Instantly share code, notes, and snippets.

@limboinf
Last active August 19, 2016 10:46
Show Gist options
  • Save limboinf/aa99599b34861ed0ff13b6629ac88d12 to your computer and use it in GitHub Desktop.
Save limboinf/aa99599b34861ed0ff13b6629ac88d12 to your computer and use it in GitHub Desktop.
轻松实现并行机制, 只要异步即可.下面是并行统计一个目录下所有文件的单词数.
/*
轻松实现并行机制,统计单词个数
目录结构如下:
├── word_count.js
└── text
├── a.txt
├── b.txt
└── c.txt
> node word_count.js
beginman: 1
hi: 2
jack: 2
good: 3
my: 1
*/
var fs = require('fs'),
completedTasks = 0,
tasks = [],
wordCounts = {},
filesDir = './text';
function checkIfComplete(){
completedTasks++;
if( completedTasks == tasks.length) {
for(var index in wordCounts) {
console.log(index + ': ' + wordCounts[index]);
}
}
}
function countWord(text) {
var words = text.toString().toLowerCase().split(/\W+/).sort();
for (var index in words) {
var word = words[index];
if (word) {
wordCounts[word] = wordCounts[word] ? wordCounts[word] + 1 : 1;
}
}
}
fs.readdir(filesDir, function (err, files) {
if (err) throw err;
for(var index in files) {
var filePath = filesDir + '/' + files[index];
// 定义任务,由于readFile是异步的,则可并行执行
var task = (function (file) {
return function () {
fs.readFile(file, function (err, data) {
if (err) throw err;
countWord(data);
checkIfComplete();
})
}
})(filePath);
// 把所有任务都添加到数组中
tasks.push(task);
}
// 开始并行执行所有任务
for (var task in tasks) {
tasks[task]();
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment