[英]Best way to share variables in node processes
Suppose I have 2 two processes like these:假设我有两个这样的过程:
let variable1 = "variable1"
let variable2 = "variable2"
that have both been spawned using都使用
node file1.js
node file2.js
Is there a way to let them communicate?有没有办法让他们交流? For example can I get variable1
's value from file2.js
?例如,我可以从file2.js
获取variable1
的值吗?
If you create a master node js and fork
2 child node js process than you can communicate data between parent and child
.如果您创建一个主节点 js 并fork
2 个子节点 js 进程,那么您可以在parent and child
之间进行数据通信。
Basic example:基本示例:
const fork = require('child_process').fork;
const program = path.resolve('child.js');
const parameters = [];
const options = {
stdio: [ 'pipe', 'pipe', 'pipe', 'ipc' ]
};
const child = fork(program, parameters, options);
child.on('message', message => {
console.log('message from child:', message);
child.send('Hi');
});
More: https://nodejs.org/api/child_process.html#child_process_child_process_fork_modulepath_args_options更多: https://nodejs.org/api/child_process.html#child_process_child_process_fork_modulepath_args_options
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.