lah*_*mal 19 javascript node.js node-cluster
似乎所有节点woker进程都在运行,就像它正在执行同一应用程序的新副本一样.但是希望保留节点集群中所有节点工作者(子进程)共享的一些变量.有一个简单的方法吗?
小智 12
您可以尝试在主进程和子进程之间进行通信.例如:
script test.master.js:
var cluster = require('cluster');
var childScript = __dirname + '/test.child.js';
cluster.setupMaster({ exec: childScript });
proc = cluster.fork();
proc.on('message', function(message) {
console.log('message from child: ', message);
proc.send('Hello from master!');
});
Run Code Online (Sandbox Code Playgroud)
脚本test.child.js:
console.log('Child initializing..');
process.on('message', function(message) {
console.log('message from master: ', message);
});
process.send('Hello from Child!');
Run Code Online (Sandbox Code Playgroud)
我为此使用了外部内存缓存或Redis服务器。
| 归档时间: |
|
| 查看次数: |
16636 次 |
| 最近记录: |