Now we shall look into various options Node provides, for creating and controlling other processes. The use cases for this are:
1) Node Event Loop is very efficient for I/O intensive tasks, but if there arises a CPU intensive task, it can still hold up the event loop. To avoid this, we should run a separate process for the same.
2) We simply want to run an external utility using Node and get back the results.
There are two approaches to creating, executing, and controlling child processes:
child_process.exec()
The exec function has the following signature:
child_process.exec('command', optionsObject, function callback(err, stdout, stderr){
});
command - the text of the command that we would run in the terminal
optionsObject - an object containing various parameters - cwd, encoding, timeout, killSignal, maxBuffer, env
callback function - the function that is executed when the process returns. The function is given 3 parameters - error, and the output of standard and error streams
The exec() function is a quick way to get tasks done. However it has some limitations, namely:
1. Cannot communicate with the child process except command line args and environment variables
2. Output is buffered, so it can't be streamed
To address these limitations, we have another method that provides us much fine grained control, which is:
child_process.spawn()
The spawn() function has the following signature:
var proc = child_process.spawn('command', [parameters]);
It takes in a command name and an array of command line parameters as argument. And returns a ChildProcess object which is an encapsulation of the process and allows to communicate with and control it.
Communication with the child process:
1) Reading
Every ChildProcess object has a stdout property that represents the standard output of the child process. We can attach a "data" event handler on the stream to process the data sent by the process in following way:
child.stdout.on('data', function(data) {
console.log(data);
})
2) Writing
We can also send the data to the child process using the write() function of the stdin stream of ChildProcess object.
3) On process end
child.on('exit', function(code, signal)) can be used to listen to process end.
4) Killing a process
You can kill a process with child.kill(signal) function, which will also pass in a signal. If the signal cannot be handled by the process, it will terminate.
1) Node Event Loop is very efficient for I/O intensive tasks, but if there arises a CPU intensive task, it can still hold up the event loop. To avoid this, we should run a separate process for the same.
2) We simply want to run an external utility using Node and get back the results.
There are two approaches to creating, executing, and controlling child processes:
child_process.exec()
The exec function has the following signature:
child_process.exec('command', optionsObject, function callback(err, stdout, stderr){
});
command - the text of the command that we would run in the terminal
optionsObject - an object containing various parameters - cwd, encoding, timeout, killSignal, maxBuffer, env
callback function - the function that is executed when the process returns. The function is given 3 parameters - error, and the output of standard and error streams
The exec() function is a quick way to get tasks done. However it has some limitations, namely:
1. Cannot communicate with the child process except command line args and environment variables
2. Output is buffered, so it can't be streamed
To address these limitations, we have another method that provides us much fine grained control, which is:
child_process.spawn()
The spawn() function has the following signature:
var proc = child_process.spawn('command', [parameters]);
It takes in a command name and an array of command line parameters as argument. And returns a ChildProcess object which is an encapsulation of the process and allows to communicate with and control it.
Communication with the child process:
1) Reading
Every ChildProcess object has a stdout property that represents the standard output of the child process. We can attach a "data" event handler on the stream to process the data sent by the process in following way:
child.stdout.on('data', function(data) {
console.log(data);
})
2) Writing
We can also send the data to the child process using the write() function of the stdin stream of ChildProcess object.
3) On process end
child.on('exit', function(code, signal)) can be used to listen to process end.
4) Killing a process
You can kill a process with child.kill(signal) function, which will also pass in a signal. If the signal cannot be handled by the process, it will terminate.
No comments:
Post a Comment