Execute the function in a new node process

I'm looking for a way (an npm or lib module would be really helpful) to run a javascript function in a new process. However, I don't want to define this function in another file. I am looking for something like a POSIX mechanism fork

.

how can i achieve this in node?

+3


source to share


1 answer


You can unblock new processes using child_process.fork . It accepts a separate module, but if you must contain it in one file, you can specify your script parameter:



var child_process = require('child_process');
var mode = process.argv[2] ? process.argv[2] : 'default';

var modes = {
    default: function() {
        console.log('I am the default, I will fork a child');
        child_process.fork(__filename, ['child']);
    },
    child: function() {
        console.log('I am the child!');
    }
};

modes[mode]();

      

+4


source







All Articles