【node.js】一度に実行する非同期処理の数を制限して、何回かに分けて処理する
Essential Node.js patterns and snippets
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
exports.Pile = function() { this.pile = []; this.concurrency = 0; this.done = null; this.max_concurrency = 10; } exports.Pile.prototype = { add: function(callback) { this.pile.push(callback); }, run: function(done, max_concurrency) { this.done = done || this.done; this.max_concurrency = max_concurrency || this.max_concurrency; var target = this.pile.length; var that = this; var next = function() { that.concurrency--; (--target == 0 ? that.done() : that.run()); }; while(this.concurrency < this.max_concurrency && this.pile.length > 0) { this.concurrency++; var callback = this.pile.shift(); callback(next); } } }; |
使用例)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
var pile = require("./pile.js").Pile; var pilex = new pile(); var counter = 0; for(var i = 0; i < 50; i++) { pilex.add( function test(next) { //実行する非同期処理を登録 setTimeout( function() { counter++; console.log(counter +" Hello world"); next(); }, 1500); } ); } pilex.run(function() { //登録した処理を実行 console.log("Done "+counter); }, 5); //一度に実行する非同期処理の制限数 |
サンプル