我在创建子进程时传递参数
if (cluster.isMaster) {
for (var i = 0; i < os.cpus().length; i++) {
var new_worker_env = {};
new_worker_env["WORKER_NAME"] = "worker" + i;
var new_worker = cluster.fork(new_worker_env);
}
}
然后尝试在孩子中阅读:
if ( process.env["WORKER_NAME"] != undefined ) instance.name = process.env["WORKER_NAME"];
但这个var不存在,为什么?
节点v0.8.8
答案 0 :(得分:14)
似乎在Windows上为我工作,Node.js版本0.8.8
var cluster = require('cluster'),
os = require('os');
if (cluster.isMaster) {
for (var i = 0; i < os.cpus().length; i++) {
var new_worker_env = {};
new_worker_env["WORKER_NAME"] = "worker" + i;
var new_worker = cluster.fork(new_worker_env);
}
} else {
console.log(process.env['WORKER_NAME']);
}
输出:
worker0
worker1