Node.js Multi-process Node.js runs in single-threaded mode, using events to handle concurrency.
exec()
Use the execution commands of the child process to cache the output of the child process. The output of the child process is returned as a callback function parameter
process.argv()
When the parameter is 0
Is the absolute address of a node file
When the parameter is 1
Is the absolute address of the file
When the parameter is 2
Is the first argument to the command.
child_process.exec
The callback function has three options: error, stdout, stderr error for program execution error, normal execution will return a null stdout for program normal output stderr for program error output
/*master.js*/
const fs = require('fs');
const child_process = require('child_process');
for(var i=0; i<3; i++) {
var count = 0;
var workerProcess = child_process.exec('node support.js ' +i, function (err, std, stderr) {
if (err) {
console.log(err.stack);
console.log('Error code: '+err.code);
console.log('Signal received: '+err.signal);
}
console.log('-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -');
console.log(i);
console.log('stdout: ' + std);
console.log('stderr: ' + stderr);
});
workerProcess.on('exit'.function (code) {
console.log('Child process has exited, exit code'+code);
console.log('Order of execution' + count++);
});
}
Copy the code
/*support.js*/
/*support.js*/
console.log("Process" + process.argv[2] + "Execute." );
Copy the code
The execution result
PS C:\Users\mingm\Desktop\test> node master. Js child process has quit, exit code execution sequence 0 0 -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - 3 stdout: process 0. Stderr: the child has quit, exit code 0 execution order 1 -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - 3 stdout: process 1. Stderr: the child has quit, exit code 0 execution order 2 -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- - 3 stdout: 2 execution process. stderr: PS C:\Users\mingm\Desktop\test>
Copy the code
Node.js executes asynchronously, resulting in a loop of three commits each time. So the output I is all 3 because the child process is running first, and when the child process is finished, the exit event is triggered and executed
console.log('Child process has exited, exit code'+code);
console.log('Order of execution' + count++);
Copy the code
The two sentences. Then perform
console.log('-- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -');
console.log(i);
console.log('stdout: ' + std);
console.log('stderr: ' + stderr);
Copy the code
The execution is complete. The biggest feature of Node is asynchronous execution.
Spawn () method
A new process is also created using the specified command line.
PS C:\Users\mingm\Desktop\test> node master.js stdout process 0 executes. Stdout process 1 executes. The child process exits. 0 stdout process 2 executes. Child process exits 0 Child process exits 0 PS C:\Users\mingm\Desktop\test> node master.js stdout process 0 executes. The child process exits. 0 Stdout Process 1 executes. Stdout process 2 executes. Child process exits 0 Child process exits 0 PS C:\Users\mingm\Desktop\test> node master.js stdout process 0 executes. Stdout process 1 executes. The child process exits. 0 stdout process 2 executes. Child process exits 0 Child process exits 0 PS C:\Users\mingm\Desktop\test>
Copy the code
Node performs asynchrony quite magically and erratically
/*master.js*/
const fs = require('fs');
const child_process = require('child_process');
for(var i=0; i<3; i++) {
var workerProcess = child_process.spawn('node'['support.js', i]); // Bind two events to workerProcess.stdout.on('data', (data) => {
console.log('stdout' + data);
});
workerProcess.stderr.on('data', (data) => {
console.log('stderr', + data); }); // Set the exit event workerprocess. on('close', (code) => {
console.log('Child process exits' + code);
});
};
Copy the code
/*support.js*/
console.log("Process" + process.argv[2] + "Execute." );
Copy the code
The difference with exec() is that exec() is a direct callback function, while spawn() is a directly bound event
Fork () method
PS C:\Users\mingm\Desktop\test> node master.js process 0 executes. Process 1 executes. Process 2 executes. Child process exited 0 Child process exited 0 child process exited 0 PS C:\Users\mingm\Desktop\test>
Copy the code
const fs = require('fs');
const child_process = require('child_process');
for(var i=0; i<3; i++) {
var worker_process = child_process.fork('support.js', [i]);
worker_process.on('close', (code) => {
console.log('Child process has exited' + code);
});
}
Copy the code
Prefer the fork method. Fork is convenient. There’s not that much asynchrony to worry about.