11

我在创建 chlid 进程时传递参数

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"];

但是这个变量不存在,为什么?

节点 v0.8.8

4

1 回答 1

16

似乎适用于 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
于 2012-09-19T18:35:38.270 回答