-
-
Save GarconsdeCrystal/a6ee060dd5694fc24f59b84a243d5ec8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env node | |
var threadCount = 4; | |
var threadPool = require('webworker-threads').createPool(threadCount); | |
threadPool.load('gistfile2.js'); | |
threadPool.all.emit('init'); | |
threadPool.on('message', function threadMessage(data) { | |
console.log(this.id + ': ' + data); | |
}); | |
// Every 0.2 seconds we need to check | |
setInterval( checkForJobs, 200 ); | |
// Every 0.1 second we need to enqueue | |
setInterval( enqueueJob, 100 ); | |
function enqueueJob () { | |
threadPool.any.eval('doEncode()', function(err, ok){ | |
console.log("RESULT:"); | |
console.log(err, ok); | |
}); | |
} | |
/** | |
* | |
*/ | |
function checkForJobs() { | |
var jobs = Array(); | |
if (threadPool.idleThreads() == 0) { | |
/* We have no threads to run the encode on so dont bother */ | |
console.log('!!! No threads'); | |
return; | |
} | |
else { | |
console.log('idle threads: ', threadPool.idleThreads()); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Worker thread */ | |
/** | |
* This is the worker module that will be used to apply | |
* filters to lists of frame images then reencode the images | |
* to video | |
*/ | |
function doEncode() { | |
var i = 0, g = 0; | |
console.log('[' + thread.id + '] Starting encode '); | |
while (i++ < 10000000) { | |
if (i == 200) { | |
console.log('-- here 200'); | |
} | |
if (i == 400) { | |
console.log('-- here 400'); | |
} | |
if (i == 500) { | |
console.log('-- here 500'); | |
} | |
g = t(i) * t(i+1); | |
} | |
console.log('++++ done ' + g); | |
return g; | |
} | |
thread.on('init', function onInit() { | |
console.log('[' + this.id + '] Starting thread'); | |
}); | |
function t(i) { return i / 65 * 3; } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment