UNPKG

virtual-worker-pool

Version:

Virtual async workers

94 lines (79 loc) 1.91 kB
// Using timeouts, create a worker pool var redis = require('redis'); var client = redis.createClient(); var EventEmitter = require('events').EventEmitter; events = new EventEmitter(); events.on('thread:done', function() { var allThreadsDone = true; threads.forEach(function(t) { if (!t.done) { allThreadsDone = false; } }); if (allThreadsDone) { end = Date.now(); var diff = end - start; console.log('Thread Pool finished at ', diff / 1000, 'seconds'); client.quit(); } }); var vThread = function(id) { this.id = id || 'Anon vThread'; this.tout = null; this.count = 0; this.totalRecords = 0; }; vThread.prototype.run = function() { var self = this; //clearTimeout(self.tout); self.count++; //console.log(self.id, ' cycle'); client.get('item', function(err, item) { var j = JSON.parse(item); if (self.count > 1000) { self.totalRecords += self.count; console.log('Processed ', self.totalRecords, ' records so far'); self.count = 0; //self.done = true; //events.emit('thread:done', {}); self.cycle(); } else { self.cycle(); } }); }; vThread.prototype.cycle = function() { var self = this; // self.tout = setTimeout(function() { // self.run(); // }, 0); setImmediate(function() { self.run(); }); }; var threads = []; threads.push(new vThread('Micheal')); threads.push(new vThread('JP')); threads.push(new vThread('Bart')); threads.push(new vThread('Rachel')); threads.push(new vThread('Isaac')); // var start = Date.now(); var end = null; console.log('starting ', new Date()); threads.forEach(function(t) { t.cycle(); }); // var c = 0; // var b = ''; // var tt = function() { // c++; // for (var a = 1; a < 1000000; a++) { // if (a === 0) { // b = a; // } // } // console.log('cycle ', c); // setImmediate(tt); // } // // setImmediate(tt);