[英]Fork a child process and inject dependency
我目前在一个阻塞的模块中有一个操作,所以我正在考虑把它变成一个我代替的子进程。
如果我想这样做,那么我当然需要修改我的模块的架构。 该模块要求通过将模块作为函数调用来注入依赖项,并传入依赖项,如下所示:
var dependency = { name: "Bob" }
require('worker')(dependency)
然后在我的worker
模块中:
module.exports = function (dependency) {
// Outputs { name: "Bob" }
console.log(dependency)
}
如何将此示例转换为分叉的子进程?
使用.fork()时,您正在启动一个完全独立的进程,因此您无法在父进程和子进程之间传递引用(并且仅限于创建进程后的消息传递)。
不需要消息传递的方法是在分叉进程时传递参数(在数组中)。 虽然我相信你必须坚持使用简单的字符串/数字值(但看起来这对你来说可能已经足够了)。 例如。:
在顶层:
var name = 'bob'
var args = [name];
var childProcess = require('child_process').fork(__dirname + '/worker', args);
在工作过程中:
var name = process.argv[2]; //AFIAK elements 0 and 1 are already populated with env info
更新
如果你真的想要去消息传递路径(如果你已经需要发送消息我会犹豫推荐),那么你可以区分这样的消息类型(可能有更优雅的方式):
在顶层:
var childProcess = require('child_process').fork(__dirname + '/worker');
childProcess.send({msgtype:'dependencies', content:dependencies});
//Then to send 'normal' message:
childProcess.send({msgtype:'myothermessagetype', content:'some content'}
在工作过程中:
process.on('message', function(msg){
if(msg.mtype == 'dependencies') {
var dependencies = msg.content;
//Do something with dependencies
} else if(msg.mtype == 'myothermessagetype') {
var normalmessage = msg.content;
//Do something in response to normal message.
}
});
a.js
var fork = require ("child_process").fork;
var child;
var init = false;
var m = module.exports = {};
m.init = function (o){
if (init) return;
init = true;
child = fork (__dirname + "/child");
child.send ({ init: o });
};
m.print = function (o){
if (!init) return;
child.send ({ msg: o });
};
m.uninit = function (){
if (!init) return;
child.on ("exit", function (){
init = false;
});
child.kill ();
};
child.js
var dependency;
var print = function (o){
console.log (o + dependency.name);
};
process.on ("message", function (o){
if (o.init){
dependency = o.init;
}else{
print (o.msg);
}
});
b.js
var a = require ("./a");
a.init ({ name: "asd" });
a.print ("hi, ");
setTimeout (function (){
a.uninit ();
}, 1000);
印刷品:嗨,asd
在主模块中:
var dependency = {message: 'hello there'};
var args = [JSON.stringify(dependency)];
var child = require('child_process').fork('worker', args);
child.send('sayhello');
child.send('exit');
并在子进程模块(worker.js)中:
var dependency = JSON.parse(process.argv[2]);
process.on('message', function(m){
if(m == 'sayhello') console.log(dependency.message);
else if(m == 'exit') process.exit();
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.