如何保留全局变量值

时间:2014-06-06 07:54:49

标签: node.js

我在一个名为File1.js的文件中使用全局变量arr []。当我在节点中运行文件时,它正在工作。但是在节点中使用Cluster模块它不起作用。任何有关这方面的帮助都会非常有用。

//Cluster.js
var cluster = require('cluster');
var workers = process.env.WORKERS || require('os').cpus().length;

if (cluster.isMaster) {
    winston.log("info", "start cluster with %s workers" + workers);
    // Create a worker for each CPU
    for (var i = 0; i < workers; i++) {
        var options = { exec: "File1.js" };
        cluster.setupMaster(options);
        var worker = cluster.fork().process;
        winston.log("info", "worker %s started." + worker.pid);
        worker.on('death', function (worker) {
            winston.log("debug", "worker " + worker.pid + " died. restart...");
            cluster.fork();
        });
        worker.on('message', function (message) {
            // error in message.err
            winston.log("error", "Error Message: " + message.err);
        });
    }
}

if (cluster.isWorker) {
    process.on('uncaughtException', function (err) {
        // tell the master there is an error
        //process.send({ err: err });
        winston.log("error", "Error Message: " + err.stack);
    });
}

}

我将使用将调用File1.js

的节点Cluster.js

0 个答案:

没有答案