当编写nodejs多线程包时,主线程可以通过fd:3发送内容并且线程可以接收消息时出现问题,但是线程无法通过fd发回任何内容:3
我做错了吗? (line threader.js:45-59是问题表明它的自我)
Package(现在只有在github上我才能使包工作)
启动代码:
var Thread = require("threader");
var task = Thread.task(function(){
//Do some calculation
}, function(){
//When the calculation response has been sent
});
task('a', 2);
答案 0 :(得分:1)
我刚刚想出了问题:
thread.js
就像一个套接字服务器,threader.js
就像一个客户端。
服务器必须在连接的上下文中响应。
由于您使用的是setTimeout,它本身是一个无法访问连接上下文的独立线程,因此穿线程序无法侦听数据。
thread.js - 旧代码
pipe.on('data', function(chunk){
console.log('RECEIVED CONENT THOUGH fd:3 in thread');
console.log(chunk.toString());
});
setTimeout(function () {
pipe.write('I piped a thing');
}, 2000);
thread.js - 新代码
pipe.on('data', function(chunk){
console.log('RECEIVED CONENT THOUGH fd:3 in thread');
console.log(chunk.toString());
});
pipe.write('I piped a thing');
或强>
thread.js - 新代码 - 最佳方式
pipe.on('data', function(chunk){
console.log('RECEIVED CONENT THOUGH fd:3 in thread');
console.log(chunk.toString());
//Real 2 second work but not on a separate thread using setTimeout
pipe.write('I piped a thing');
});
答案 1 :(得分:0)
我刚刚重新编写整个包,从另一个角度开始,现在它可以工作......
我认为问题在于挑选线程。
修复程序将很快推送到github。