我正在尝试根据我的请求创建多个线程。当我执行以下代码时,我收到错误
结果:[错误:ReferenceError:未定义doEncode] null
以下是我的问题摘要:
这是我的代码
File1.js
var threadCount = 4;
var threadPool = require('webworker-threads').createPool(threadCount);
threadPool.load('File2.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);
if(ok)
threadPool.destroy();
});
}
function checkForJobs() {
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());
}
}
File2.js
var value = require("./common");
function File2() {
}
File2.prototype.doEncode = function () {
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 = value.t(i) * value.t(i + 1);
}
console.log('++++ done ' + g);
return g;
}
module.exports = File2
common.js
function t(i) {
return i / 65 * 3;
}
exports.t = t;
任何人都可以帮助我,我在这里做错了,还是可以通过使用child_process实现同样的目标?哪种方法可以在没有内存泄漏的情况下提供更好的性能?