Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

node.js fork function and passing arguments to child-process

Tags:

node.js

I pass arguments when create chlid-processes

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);
    }
}

and then try to read it in childs:

if ( process.env["WORKER_NAME"] != undefined ) instance.name = process.env["WORKER_NAME"];

but this var isn't exist, why?

Node v0.8.8

like image 528
ElSajko Avatar asked Sep 19 '12 13:09

ElSajko


1 Answers

Seems to work for me on Windows, Node.js version 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']);
}

outputs:

worker0
worker1
like image 56
Jason Brumwell Avatar answered Sep 28 '22 02:09

Jason Brumwell