how to create child process in nodejs
Child Process can be created in NodeJS by various ways.
1. Fork
2. Spawn
When child process is getting created, the PPID of the child process will be the PID of the parent process
Child.js
var sleep=require("sleep");
while(1){
console.log(" I m child process");
sleep.sleep(1);
}
Parent.js [ Via Fork ]
const spawnObj = require('child_process');
const child = spawnObj.fork('/home/user1/test/child.js');
This will create a child process. But you cannot have control over the input / output / error streams of the child process
Parent.js [ Via Spawn]
const spawnObj = require('child_process');
var cmd="/home/user1/test/child.js";
var child = spawnObj.spawn("node",[cmd], {detached: false});
child.stdout.on('data', function (data) {
console.log('stdout: ' + data);
});
child.stderr.on('data', function (data) {
console.log('stderr: ' + data);
});
.
Child Process can be created in NodeJS by various ways.
1. Fork
2. Spawn
When child process is getting created, the PPID of the child process will be the PID of the parent process
Child.js
var sleep=require("sleep");
while(1){
console.log(" I m child process");
sleep.sleep(1);
}
Parent.js [ Via Fork ]
const spawnObj = require('child_process');
const child = spawnObj.fork('/home/user1/test/child.js');
This will create a child process. But you cannot have control over the input / output / error streams of the child process
Parent.js [ Via Spawn]
const spawnObj = require('child_process');
var cmd="/home/user1/test/child.js";
var child = spawnObj.spawn("node",[cmd], {detached: false});
child.stdout.on('data', function (data) {
console.log('stdout: ' + data);
});
child.stderr.on('data', function (data) {
console.log('stderr: ' + data);
});
.
No comments:
Post a Comment